|
Timer still crashing >.< |
Angeljruiz
Member #14,553
September 2012
|
1//
2// main.cpp
3// Snake
4//
5// Created by Angel on 9/25/12.
6// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7//
8
9#include <allegro5/allegro.h>
10#include <allegro5/allegro_primitives.h>
11#include <cstdlib>
12#include <time.h>
13#include <vector>
14
15#include <iostream>
16
17using namespace std;
18
19enum dir{ LEFT, UP, RIGHT, DOWN };
20
21struct Tile {
22 public:
23 bool On;
24 bool Special;
25 int x,y;
26};
27class Snake {
28public:
29 vector<Tile> vTiles;
30};
31
32bool isWall(int,int);
33void SetupTiles(Tile Tiles[][15]);
34void GenTile(Tile Tiles[][15]);
35void Move(Snake,Tile Tiles[][15],dir);
36
37int main(int argc, char **argv)
38{
39 srand((unsigned int)time(NULL));
40
41 ALLEGRO_DISPLAY *display = NULL;
42 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
43 ALLEGRO_TIMER *timer = NULL;
44
45 bool Exit = false;
46 bool Redraw = true;
47
48
49 Tile Tiles[20][15] = { false, false, 0, 0 };
50
51 for (int x = 0; x < 20; ++x)
52 {
53 for (int y = 0; y < 15; ++y)
54 {
55 Tiles[x][y].x = x;
56 Tiles[x][y].y = y;
57 }
58 }
59
60 SetupTiles(Tiles);
61 GenTile(Tiles);
62
63 Snake aSnake;
64
65 aSnake.vTiles.push_back(Tiles[2][4]);
66
67 Move(aSnake, Tiles, LEFT);
68
69
70 al_init();
71 al_install_keyboard();
72 al_init_primitives_addon();
73
74 timer = al_create_timer(1.0 / 120);
75 display = al_create_display(640, 480);
76 event_queue = al_create_event_queue();
77
78 al_register_event_source(event_queue, al_get_display_event_source(display));
79 al_register_event_source(event_queue, al_get_keyboard_event_source());
80 al_register_event_source(event_queue, al_get_timer_event_source(timer));
81
82
83 al_start_timer(timer);
84
85
86 while (!Exit)
87 {
88 ALLEGRO_EVENT ev;
89 al_wait_for_event(event_queue, &ev);
90
91 if (ev.type == ALLEGRO_EVENT_KEY_UP)
92 {
93 switch (ev.keyboard.keycode)
94 {
95 case ALLEGRO_KEY_ESCAPE:
96 Exit = true;
97 break;
98
99 case ALLEGRO_KEY_A:
100 GenTile(Tiles);
101 Move(aSnake, Tiles, LEFT);
102 Redraw = true;
103 break;
104 }
105 }
106
107 if (ev.type == ALLEGRO_EVENT_TIMER)
108 {
109 //Redraw = true;
110 }
111 //exit(-5);
112 if (al_event_queue_is_empty(event_queue) && Redraw)
113 {
114 vector<Tile>::iterator it;
115 al_clear_to_color(al_map_rgb(245, 245, 245));
116 for (int x = 0; x < 20; ++x)
117 {
118 for (int y = 0; y < 15; ++y)
119 {
120 if (Tiles[x][y].On)
121 al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(0, 0, 0));
122 if (Tiles[x][y].Special)
123 al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(255, 0, 0));
124 if (x)
125 al_draw_line(x*32, 0, x*32, 480, al_map_rgb(220, 220, 220), 3);
126 if (y)
127 al_draw_line(0, y*32, 640, y*32, al_map_rgb(220, 220, 220), 3);
128 }
129 }
130 for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
131 al_draw_filled_rectangle(it->x*32, it->y*32, it->x*32+32, it->y*32+32, al_map_rgb(0, 0, 0));
132 }
133 al_flip_display();
134 Redraw = false;
135 }
136
137
138 }
139
140 if (display) al_destroy_display(display);
141 if (event_queue) al_destroy_event_queue(event_queue);
142 if (timer) al_destroy_timer(timer);
143
144
145
146 return -1;
147}
148
149bool isWall(int x, int y)
150{
151 if (x == 0 || y == 0)
152 return true;
153 if (x == 19)
154 return true;
155 if (y == 14)
156 return true;
157 return false;
158}
159void SetupTiles(Tile Tiles[][15])
160{
161 for (int x = 0; x < 20; ++x)
162 {
163 Tiles[x][0].On = true;
164 }
165 for (int y = 0; y < 15; ++y)
166 {
167 Tiles[19][y].On = true;
168 }
169 for (int x = 19; x >= 0; --x)
170 {
171 Tiles[x][14].On = true;
172 }
173 for (int y = 14; y >= 0; --y)
174 {
175 Tiles[0][y].On = true;
176 }
177}
178void GenTile(Tile Tiles[][15])
179{
180 int tX = rand() % 18 + 1;
181 int tY = rand() % 13 + 1;
182 if (Tiles[tX][tY].On || Tiles[tX][tY].Special)
183 GenTile(Tiles);
184 Tiles[tX][tY].Special = true;
185}
186void Move(Snake aSnake, Tile Tiles[][15], dir Direction)
187{
188 vector<Tile>::iterator it;
189 for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
190 if (it == aSnake.vTiles.begin())
191 {
192 switch (Direction) {
193 case LEFT:
194 it->x -= 1;
195 break;
196
197 case UP:
198 break;
199
200 case RIGHT:
201 break;
202
203 case DOWN:
204 break;
205 }
206 }
207 }
208}
Destroying the timer still crashes program everyonce in awhile >.< |
Thomas Fjellstrom
Member #476
June 2000
|
See what happens if you destroy the event queue last. -- |
Angeljruiz
Member #14,553
September 2012
|
Alright - Ill see how it goes |
weapon_S
Member #7,859
October 2006
|
That isn't supposed to solve the problem. If that works there is a bug in Allegro. Report back, please. Try to link the debug version then. |
Angeljruiz
Member #14,553
September 2012
|
Its still crashing ._. What do you mean link the debug version ? 1
2//
3// main.cpp
4// Snake
5//
6// Created by Angel on 9/25/12.
7// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
8//
9
10#include <allegro5/allegro.h>
11#include <allegro5/allegro_primitives.h>
12#include <cstdlib>
13#include <time.h>
14#include <vector>
15
16#include <iostream>
17
18using namespace std;
19
20enum dir{ LEFT, UP, RIGHT, DOWN };
21
22struct Tile {
23 public:
24 bool On;
25 bool Special;
26 int x,y;
27};
28class Snake {
29public:
30 vector<Tile> vTiles;
31};
32
33bool isWall(int,int);
34void SetupTiles(Tile Tiles[][15]);
35void GenTile(Tile Tiles[][15]);
36void Move(Snake &aSnake,Tile Tiles[][15],dir);
37
38int main(int argc, char **argv)
39{
40 srand((unsigned int)time(NULL));
41
42 ALLEGRO_DISPLAY *display = NULL;
43 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
44 ALLEGRO_TIMER *timer = NULL;
45
46 bool Exit = false;
47 bool Redraw = true;
48
49
50 Tile Tiles[20][15] = { false, false, 0, 0 };
51
52 for (int x = 0; x < 20; ++x)
53 {
54 for (int y = 0; y < 15; ++y)
55 {
56 Tiles[x][y].x = x;
57 Tiles[x][y].y = y;
58 }
59 }
60
61 SetupTiles(Tiles);
62 GenTile(Tiles);
63
64 Snake aSnake;
65
66 aSnake.vTiles.push_back(Tiles[2][4]);
67 aSnake.vTiles.push_back(Tiles[2][4]);
68
69
70
71 al_init();
72 al_install_keyboard();
73 al_init_primitives_addon();
74
75 timer = al_create_timer(1.0 / 120);
76 if (!timer)
77 exit(-1);
78 display = al_create_display(640, 480);
79 event_queue = al_create_event_queue();
80
81 al_register_event_source(event_queue, al_get_display_event_source(display));
82 al_register_event_source(event_queue, al_get_keyboard_event_source());
83 al_register_event_source(event_queue, al_get_timer_event_source(timer));
84
85
86 al_start_timer(timer);
87
88
89 while (!Exit)
90 {
91 ALLEGRO_EVENT ev;
92 al_wait_for_event(event_queue, &ev);
93
94 if (ev.type == ALLEGRO_EVENT_KEY_UP)
95 {
96 switch (ev.keyboard.keycode)
97 {
98 case ALLEGRO_KEY_ESCAPE:
99 Exit = true;
100 break;
101
102 case ALLEGRO_KEY_A:
103 GenTile(Tiles);
104 Move(aSnake, Tiles, RIGHT);
105 Redraw = true;
106 break;
107 }
108 }
109
110 if (ev.type == ALLEGRO_EVENT_TIMER)
111 {
112 //Redraw = true;
113 }
114 //exit(-5);
115 if (al_event_queue_is_empty(event_queue) && Redraw)
116 {
117 vector<Tile>::iterator it;
118 al_clear_to_color(al_map_rgb(245, 245, 245));
119 for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
120 al_draw_filled_rectangle(it->x*32, it->y*32, it->x*32+32, it->y*32+32, al_map_rgb(0, 0, 0));
121 }
122 for (int x = 0; x < 20; ++x)
123 {
124 for (int y = 0; y < 15; ++y)
125 {
126 if (Tiles[x][y].On)
127 al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(0, 0, 0));
128 if (Tiles[x][y].Special)
129 al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(255, 0, 0));
130 if (x)
131 al_draw_line(x*32, 0, x*32, 480, al_map_rgb(220, 220, 220), 3);
132 if (y)
133 al_draw_line(0, y*32, 640, y*32, al_map_rgb(220, 220, 220), 3);
134 }
135 }
136 al_flip_display();
137 Redraw = false;
138 }
139
140
141 }
142
143 if (display) al_destroy_display(display);
144 if (timer) al_destroy_timer(timer);
145 if (event_queue) al_destroy_event_queue(event_queue);
146
147
148
149 return -1;
150}
151
152bool isWall(int x, int y)
153{
154 if (x == 0 || y == 0)
155 return true;
156 if (x == 19)
157 return true;
158 if (y == 14)
159 return true;
160 return false;
161}
162void SetupTiles(Tile Tiles[][15])
163{
164 for (int x = 0; x < 20; ++x)
165 {
166 Tiles[x][0].On = true;
167 }
168 for (int y = 0; y < 15; ++y)
169 {
170 Tiles[19][y].On = true;
171 }
172 for (int x = 19; x >= 0; --x)
173 {
174 Tiles[x][14].On = true;
175 }
176 for (int y = 14; y >= 0; --y)
177 {
178 Tiles[0][y].On = true;
179 }
180}
181void GenTile(Tile Tiles[][15])
182{
183 int tX = rand() % 18 + 1;
184 int tY = rand() % 13 + 1;
185 if (Tiles[tX][tY].On || Tiles[tX][tY].Special)
186 GenTile(Tiles);
187 Tiles[tX][tY].Special = true;
188}
189void Move(Snake &aSnake, Tile Tiles[][15], dir Direction)
190{
191 vector<Tile>::iterator it;
192 vector<Tile>::iterator it2;
193 for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
194 if (it == aSnake.vTiles.begin())
195 {
196 it2 = it;
197 switch (Direction) {
198 case LEFT:
199 it->x -= 1;
200 break;
201
202 case UP:
203 it->y -= 1;
204 break;
205
206 case RIGHT:
207 it->x += 1;
208 break;
209
210 case DOWN:
211 it->y += 1;
212 break;
213 }
214 } else {
215 it->x = it2->x;
216 it->y = it2->y;
217 it2 = it;
218 }
219 }
220}
|
|