Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Game Designs

This thread is locked; no one can reply to it. rss feed Print
Game Designs
j0rdant13
Member #14,974
March 2013

First of all, i know this is a general c++ question but i am using allegro and this questions is to do with not just allegro but any future game libraries/languages.

I would say i can finally say i know quite alot about c++ and allegro. What i need now is to know how to implement what i know into code, and before you say start off small and work up to larger more complex programs i think i can say i have been doing that with small games etc.

The problem i now face is that i have been reading about the general design of a game. I have read many articles/posts online explaining different designs.

Here is why i need help, i have read many different designs but none of which explain how to implement them into a game properly (not what i have come across anyway) so here is my question...

What game design would you suggest that manages game objects, communication. The question is a wide one so to shorten the scope:

here is the main engine:

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_font.h> 3#include <allegro5\allegro_ttf.h> 4#include <allegro5\bitmap.h> 5#include <allegro5\allegro_image.h> 6#include <allegro5\keyboard.h> 7#include <allegro5\allegro_primitives.h> 8//================================================================= 9#include <vector> 10#include <iostream> 11//================================================================= 12#include "Globals.h" 13#include "GameObject.h" 14#include "Player.h" 15#include "Ammo.h" 16#include "Bullet.h" 17//================================================================= 18int main() 19{ 20 bool running = true, redraw = false; 21 std::vector<Ammo *> ammoVector; 22 std::vector<Ammo *>::iterator iter; 23 //================================================================= 24 al_init(); 25 al_init_font_addon(); 26 al_init_ttf_addon(); 27 al_init_image_addon(); 28 al_install_keyboard(); 29 al_init_primitives_addon(); 30 //================================================================= 31 Player *player = new Player; 32 player->Load(); 33 //================================================================= 34 ALLEGRO_DISPLAY *display = al_create_display(WIDTH, HEIGHT); 35 ALLEGRO_FONT *font = al_load_ttf_font("MyFont.ttf", 20, NULL); 36 ALLEGRO_FONT *font_large = al_load_ttf_font("MyFont.ttf", 50, NULL); 37 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 38 ALLEGRO_TIMER *timer_fps = al_create_timer(1.0 / FPS); 39 ALLEGRO_TIMER *timer_anim = al_create_timer(1.0 / ANIM); 40 //================================================================= 41 al_register_event_source(event_queue, al_get_display_event_source(display)); 42 al_register_event_source(event_queue, al_get_timer_event_source(timer_fps)); 43 al_register_event_source(event_queue, al_get_timer_event_source(timer_anim)); 44 al_register_event_source(event_queue, al_get_keyboard_event_source()); 45 //================================================================= 46 47 al_start_timer(timer_fps); 48 al_start_timer(timer_anim); 49//================================================================= START OF GAME LOOP 50 while(running) 51 { 52 ALLEGRO_EVENT ev; 53 al_wait_for_event(event_queue, &ev); 54 //================================================================= CHECK IF WINDOW CLOSED 55 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 56 running = false; 57 //================================================================= CHECK IF WINDOW CLOSED 58 //================================================================= Keyboard 59 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 60 { 61 if(player != NULL) 62 player->CheckDown(ev); 63 if(player != NULL && ev.keyboard.keycode == ALLEGRO_KEY_SPACE) 64 { 65 Bullet *bullet = new Bullet; 66 bullet->Load(player->GetX(), player->GetY() + 10); 67 ammoVector.push_back(bullet); 68 } 69 if(player == NULL && ev.keyboard.keycode == ALLEGRO_KEY_R) 70 { 71 player = new Player; 72 player->Load(); 73 } 74 } 75 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 76 { 77 if(player != NULL) 78 player->CheckUp(ev); 79 } 80 //================================================================= Keyboard 81 //================================================================= TIMER EVENTS/ UPDATES 82 if(ev.type == ALLEGRO_EVENT_TIMER) 83 { 84 if(ev.timer.source == timer_fps) 85 { 86 // ======== PLAYERS ========== 87 if(player != NULL) 88 player->Update(); 89 // ======== PLAYERS ========== 90 91 // ======== OBJECTS ========== 92 for(unsigned int i = 0; i < ammoVector.size(); i++) 93 { 94 if(ammoVector[i]->GetAlive()) 95 ammoVector[i]->Update(); 96 } 97 // ======== OBJECTS ========== 98 99 redraw = true; 100 } 101 //================================================================= ANIMATION 102 if(ev.timer.source == timer_anim) 103 { 104 if(player != NULL && player->GetWalking()) 105 player->WalkAnim(); 106 } 107 //================================================================= ANIMATION 108 } 109 //================================================================= TIMER EVENTS/ UPDATES 110 //================================================================= CULL THE DEAD 111 if(player != NULL) 112 { 113 if(!player->GetAlive()) 114 { 115 player->Destroy(); 116 delete player; 117 player = NULL; 118 } 119 } 120 for(iter = ammoVector.begin(); iter != ammoVector.end();) 121 { 122 if(!(*iter)->GetAlive()) 123 { 124 (*iter)->Destroy(); 125 delete (*iter); 126 iter = ammoVector.erase(iter); 127 }else { iter++; } 128 } 129 //================================================================= CULL THE DEAD 130 //================================================================= DRAW 131 if(redraw && al_is_event_queue_empty(event_queue)) 132 { 133 // ======== Draw Player ======== 134 if(player != NULL) 135 player->Draw(); 136 // ======== Draw Player ======== 137 // ======== Draw Objects ======== 138 for(unsigned int i = 0; i < ammoVector.size(); i++) 139 ammoVector[i]->Draw(); 140 // ======== Draw Objects ======== 141 142 // ======== Draw Player Stats ======== 143 if(player != NULL) 144 { 145 al_draw_textf(font, al_map_rgb(255,255,255), 0, 0, NULL, "Score: %i", player->GetScore()); 146 al_draw_textf(font, al_map_rgb(255,255,255), 0, 20, NULL, "Health: %i", player->GetHealth()); 147 al_draw_textf(font, al_map_rgb(255,255,255), 0, 40, NULL, "Damage: %i", player->GetDamage()); 148 al_draw_textf(font, al_map_rgb(255,255,255), 0, 60, NULL, "Speed: %i", player->GetSpeed()); 149 al_draw_text(font, al_map_rgb(255,255,255), 0, 80, NULL, "-- Player Co-Ordinates --"); 150 al_draw_textf(font, al_map_rgb(255,255,255), 0, 100, NULL, "Player X: %i", player->GetX()); 151 al_draw_textf(font, al_map_rgb(255,255,255), 0, 120, NULL, "Playr Y: %i", player->GetY()); 152 }else 153 { 154 al_draw_text(font_large, al_map_rgb(255,0,0), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTER, "PLAYER IS DEAD"); 155 } 156 157 al_flip_display(); 158 al_clear_to_color(al_map_rgb(0,0,0)); 159 redraw = false; 160 } 161 //================================================================= DRAW 162 } 163//================================================================= END OF GAME LOOP 164 //=================================================================CLEAN UP OBJECTS 165 if(player != NULL) 166 { 167 player->Destroy(); 168 delete player; 169 player = NULL; 170 } 171 //=================================================================CLEAN UP ALLEGRO/ ETC 172 al_destroy_display(display); 173 al_destroy_event_queue(event_queue); 174 al_destroy_timer(timer_fps); 175 al_destroy_timer(timer_anim); 176 al_uninstall_keyboard(); 177 return 0; 178}

how would i implement a design like a entity component system?
How would it look in the main.cpp?

im not expecting a whole post that goes into detail but some psuedo code for me to think about how it works/ looks.

If you read all this thankyou! if i sound a bit confusing im sorry i just need to get my head around designs and this one in particular, if i see how it looks i would be happy!!

Thanks :DD

edit:
some of the code in the main.cpp hasnt been implemented yet, it was just to show you my simple design

l j
Member #10,584
January 2009
avatar

I don't know why everybody loves component based systems so much.
Unity uses an entity component system though.
You could try using that, it would get you on track I suppose.

j0rdant13
Member #14,974
March 2013

I know what you mean but the way i was coding a game i was like, there must be a better way.. im not saying ive coded a 100 games or whatever or i have years of experience, its that when you code a game and its not much exiting and you have more problems figuring out the design, then its not fun. With an entity system it really does make life easier so you can enjoy making games and watching your game grow. I just havnt seen one in c++ ever.. and i dont know how to implement in the slightest.

I have unity installed, what is the entity system like there? is there a main.cpp where you create an engine? sorry if i sound dumb i neverused it ;D

edit:
Think of it as this. Im experienced with c++ and eallegro, i want to create a 2D game, i am too inexperienced with designs to make something that works, when i create a game i run into problems which a good structure will probably fix

l j
Member #10,584
January 2009
avatar

Just wrote this, this is pretty much as basic as it can get.

Edit: see attachments

j0rdant13
Member #14,974
March 2013

Oh wow you dont know how much i appreciate this.. i couldnt thank you more, theres an error which prevents me from running it though:

error C3531: 'it': a symbol whose type contains 'auto' must have an initializer

for (auto it : componentMap)

I dont think 'it' has been initialized?

J-Gamer
Member #12,491
January 2011
avatar

You need to compile with c++11 support enabled.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

j0rdant13
Member #14,974
March 2013

Ive never heard this before what does this mean? first time ive come accross this

Edit:

Anyone? i really wanna compile this and figure out how it works and expirement with it, i have Microsoft Visual 2010?

l j
Member #10,584
January 2009
avatar

Somewhere in 2011 a new C++ standard was released (C++11).
That standard redefined the meaning of the auto keyword.
Before C++11 auto was basically the opposite of static. Now it means the type will automatically be chosen at compile time by using type inference.

Enabling C++11 support depends on the ide or compiler. In VS2012 it's enabled by default. In codeblocks if you use the gcc compiler you'll have to enable it in your build options (gcc command line -std=c++11). For any other ide or compiler, I've got not idea, I'd suggest you look it up.

edit: I think you might have to upgrade to VS2012

j0rdant13
Member #14,974
March 2013

Okay could i use something else other than auto?

l j
Member #10,584
January 2009
avatar

Yes.
std::unordered_map<std::type_index, Component *>::iterator

I'm not sure if VS2010 supports iterating the way I did though.

j0rdant13
Member #14,974
March 2013

Updating to visual 2012 now

Go to: