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 D
edit:
some of the code in the main.cpp hasnt been implemented yet, it was just to show you my simple design
]]>