Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Choppy graphics

This thread is locked; no one can reply to it. rss feed Print
Choppy graphics
Mishtiff
Member #15,776
October 2014

hello everyone,
I have been learning to code in Allegro 5 over the last month or so. I am a pretty amateur programmer, so I do as much as I can to learn, and expect to have several things I can fix in everything I have programmed. So, all help is greatly appreciated in this topic.

I am unsure whether or not to post my entire code via an attachment, or just post small parts of the code. I currently have a game that is fairly basic following a tutorial made on Youtube by Mike Geig. It has been fantastic help :)

My problem is that now that I am progressing past his tutorial, I am running into a Choppy game. Unsure why, however, since I feel that it does very little. It is not consistently choppy, but it lags for a bit then smooths out again (the entire time it is running though).

So without continuing my rambling, should I post my entire code as an attachment or simply just go through it with you guys with posts?

Arthur Kalliokoski
Second in Command
February 2005
avatar

Mishtiff said:

I am running into a Choppy game.

You mean it goes too slow, or skips frames? How much cpu does it take in Resource Meter?

Quote:

should I post my entire code as an attachment or simply just go through it with you guys with posts?

If it's more than maybe 300 lines it'd be better to just post relevant parts instead of expecting people to wade through the whole thing, but I get the impression you have no idea what part is at fault.

Just try one or the other, and if nobody answers very quickly it's probably that nobody who's seen your questions is sure of their answer.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Mishtiff
Member #15,776
October 2014

thank you for your reply. I have no idea where the problem is coming from. The current game is around 900 lines long.

It is currently using 44k in task manager.

It does not seem to be skipping frames, however everything "stutters" for a few seconds, then just seems to smooth out perfectly, then goes back to doing it again.]

ill post everything at the moment from start of game to finish, minus the functions that are called to.

enemies are technically the "comets" that fly from right to left, but I plan to change that later on.

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_font.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_image.h> 6#define _USE_MATH_DEFINES 7#include <math.h> 8#include "objects.h" 9 10 11 12 13//Global Variables 14enum KEYS{ UP, DOWN, LEFT, RIGHT, SPACE, ONE, MOUSE1}; 15enum CNTRLR{ LUP, LDOWN, LRIGHT, LLEFT, RUP, RDOWN, RRIGHT, RLEFT, A, B, Y, X, LB, RB, LT, RT, SELECT, START}; 16int width = 1200; 17int height = 900; 18const int NUM_BULLETS = 30; 19const int NUM_ENEMIES = 100; 20const int NUM_PLANETS = 2; 21double mousex, mousey; 22int bulletCD = 0; //timer in-between shots 23bool joystick = false; 24 25//Variables 26bool draw = false; 27bool done = false; 28const int FPS = 60; 29bool gameOver = false; 30bool keys[7] = {false, false, false, false, false, false, false}; 31bool Cntrlr[18] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}; 32int framesCur = 0, framesPS = 0, sec = 0; 33 34 35void initPlayer(Player1 &p1); 36void movePlayerUp(Player1 &p1); 37void movePlayerDown(Player1 &p1); 38void movePlayerLeft(Player1 &p1); 39void movePlayerRight(Player1 &p1); 40void updatePlayer(Player1 &p1); 41void drawPlayer(Player1 &p1); 42 43void initPlanet(Image Planets[], int size); 44void drawPlanet(Image Planets[], int size); 45void updatePlanet(Image Planets[], int size); 46 47void initBackground(Background &bg); 48void drawBackground(Background &bg); 49void updateBackground(Background &bg); 50 51void initBullet(Bullet bullet[], int size); 52void drawBullet(Bullet bullet[], int size); 53void fireBullet(Bullet bullet[], int size, Player1 &p1); 54void updateBullet(Bullet bullet[], int size); 55void collideBullet(Bullet bullet[], int bsize, Enemies enemy[], int csize, Player1 &p1); 56 57void initPulse(Pulse &pulse); 58void drawPulse(Pulse &pulse); 59void firePulse(Pulse &pulse, Player1 &p1); 60void updatePulse(Pulse &pulse, Player1 &p1); 61void collidePulse(Pulse &pulse, Enemies enemy[], int csize, Player1 &p1); 62 63void initEnemy(Enemies enemy[], int size); 64void drawEnemy(Enemies enemy[], int size); 65void startEnemy(Enemies enemy[], int size); 66void updateEnemy(Enemies enemy[], int size); 67void collideComet(Enemies enemy[], int csize, Player1 &p1); 68int returnRand(int num); 69 70void drawHealth(Player1 &p1); 71 72int main (void) 73{ 74 75 //Object Variables 76 Player1 p1; 77 Bullet bullets[NUM_BULLETS]; 78 Pulse pulseAttack; 79 Enemies enemy[NUM_ENEMIES]; 80 Image Planets[NUM_PLANETS]; 81 Background bg; 82 83 //Allegro variables 84 ALLEGRO_DISPLAY *display = NULL; 85 ALLEGRO_MONITOR_INFO info; 86 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 87 ALLEGRO_TIMER *LPS = NULL; 88 ALLEGRO_FONT *font18 = NULL; 89 90//Initialization Funtions 91if(!al_init()) 92{ 93return -1; 94} 95 96al_get_monitor_info(0, &info); 97width = info.x2 - info.x1; 98height = info.y2 - info.y1; 99display = al_create_display(width, height); 100 101if(!display) 102 return -1; 103 104al_init_primitives_addon(); 105al_install_keyboard(); 106al_install_mouse(); 107al_install_joystick(); 108al_init_font_addon(); 109al_init_ttf_addon(); 110al_init_image_addon(); 111 112font18 = al_load_font("arial.ttf", 18, 0); 113event_queue = al_create_event_queue(); 114LPS = al_create_timer(1.0 / FPS); 115 116ALLEGRO_JOYSTICK *joy = al_get_joystick(0); 117ALLEGRO_JOYSTICK_STATE joystate; 118 119if(joy == NULL) 120 joystick = false; 121else 122 joystick = true; 123 124al_register_event_source(event_queue, al_get_display_event_source(display)); 125al_register_event_source(event_queue, al_get_keyboard_event_source()); 126al_register_event_source(event_queue, al_get_mouse_event_source()); 127al_register_event_source(event_queue, al_get_joystick_event_source()); 128al_register_event_source(event_queue, al_get_timer_event_source(LPS)); 129 130 131 132 133srand(time(NULL)); 134initBackground(bg); 135initPlanet(Planets, NUM_PLANETS); 136initPlayer(p1); 137initPulse(pulseAttack); 138initBullet(bullets, NUM_BULLETS); 139initEnemy(enemy, NUM_ENEMIES); 140 141al_hide_mouse_cursor(display); 142al_start_timer(LPS); 143 144while(!done) 145{ 146 ALLEGRO_EVENT ev; 147 al_wait_for_event(event_queue, &ev); 148 if(joystick) 149 al_get_joystick_state(joy, &joystate); 150 151 if(ev.type == ALLEGRO_EVENT_TIMER) 152 { 153 draw = true; 154 sec++; 155 156 if(!keys[UP] && !keys[DOWN] && p1.picDir != 1) 157 {p1.picDir = 1;} 158 159 if(p1.expCur >= p1.expNeeded) 160 {p1.level++; 161 p1.expCur = 0; 162 p1.expNeeded += 25;} 163 164 if(p1.health == 0 && !gameOver) 165 gameOver = true; 166 167 if(keys[UP] || Cntrlr[LUP]) movePlayerUp(p1); 168 if(keys[DOWN] || Cntrlr[LDOWN]) movePlayerDown(p1); 169 if(keys[LEFT] || Cntrlr[LLEFT]) movePlayerLeft(p1); 170 if(keys[RIGHT] || Cntrlr[LRIGHT]) movePlayerRight(p1); 171 if(joystick) 172 { 173 174 float dirx = joystate.stick[1].axis[0] - 0; 175 float diry = joystate.stick[1].axis[1] - 0; 176 float angle = atan2(diry, dirx); 177 178 if(joystate.stick[1].axis[0] != 0 || joystate.stick[1].axis[1] != 0) 179 { 180 mousex = p1.x + 100 * cos(angle); 181 mousey = p1.y + 100 * sin(angle); 182 } 183 al_set_mouse_xy(display, mousex, mousey); 184 185 if(keys[SPACE] || keys[MOUSE1] || Cntrlr[RT]) 186 { 187 bulletCD++; 188 if(bulletCD == 16) 189 {fireBullet(bullets, NUM_BULLETS, p1); bulletCD = 0;} 190 } 191 if(Cntrlr[A]) 192 { 193 firePulse(pulseAttack, p1); 194 } 195 196 } 197 198 if(!joystick) 199 { 200 if(keys[SPACE] || keys[MOUSE1] || Cntrlr[RT]) 201 { 202 bulletCD++; 203 if(bulletCD == 16) 204 {fireBullet(bullets, NUM_BULLETS, p1); bulletCD = 0;} 205 } 206 } 207 208 updateBackground(bg); 209 updatePlayer(p1); 210 updatePlanet(Planets, NUM_PLANETS); 211 updatePulse(pulseAttack, p1); 212 updateBullet(bullets, NUM_BULLETS); 213 startEnemy(enemy, NUM_ENEMIES); 214 updateEnemy(enemy, NUM_ENEMIES); 215 collideBullet(bullets, NUM_BULLETS, enemy, NUM_ENEMIES, p1); 216 collideComet(enemy, NUM_ENEMIES, p1); 217 218 if (sec == FPS) 219 {framesPS = framesCur; framesCur = sec = 0;} 220 221 } 222 223 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 224 { 225 done = true; 226 } 227 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 228 { 229 switch (ev.keyboard.keycode) 230 { 231 case ALLEGRO_KEY_ESCAPE: 232 done = true; 233 break; 234 case ALLEGRO_KEY_W: 235 keys[UP] = true; 236 break; 237 case ALLEGRO_KEY_S: 238 keys[DOWN] = true; 239 break; 240 case ALLEGRO_KEY_A: 241 keys[LEFT] = true; 242 break; 243 case ALLEGRO_KEY_D: 244 keys[RIGHT] = true; 245 break; 246 case ALLEGRO_KEY_SPACE: 247 keys[SPACE] = true; 248 break; 249 case ALLEGRO_KEY_1: 250 keys[ONE] = true; 251 gameOver = true; 252 break; 253 } 254 } 255 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 256 { 257 switch (ev.keyboard.keycode) 258 { 259 case ALLEGRO_KEY_ESCAPE: 260 done = true; 261 break; 262 case ALLEGRO_KEY_W: 263 keys[UP] = false; 264 break; 265 case ALLEGRO_KEY_S: 266 keys[DOWN] = false; 267 break; 268 case ALLEGRO_KEY_A: 269 keys[LEFT] = false; 270 break; 271 case ALLEGRO_KEY_D: 272 keys[RIGHT] = false; 273 break; 274 case ALLEGRO_KEY_SPACE: 275 keys[SPACE] = false; 276 break; 277 case ALLEGRO_KEY_1: 278 keys[ONE] = false; 279 break; 280 } 281 } 282 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 283 { 284 switch (ev.mouse.button) 285 { 286 case 1: 287 keys[MOUSE1] = true; 288 break; 289 } 290 } 291 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 292 { 293 switch (ev.mouse.button) 294 { 295 case 1: 296 keys[MOUSE1] = false; 297 break; 298 } 299 } 300 301 302 if(joystick) 303 { 304 if(joystate.stick[0].axis[0] >= .3) 305 Cntrlr[LRIGHT] = true; 306 else if(joystate.stick[0].axis[0] <= -.3) 307 Cntrlr[LLEFT] = true; 308 else if(joystate.stick[0].axis[0] >= -.3 && joystate.stick[0].axis[0] <= .3 && Cntrlr[LRIGHT] || Cntrlr[LLEFT]) 309 {Cntrlr[LRIGHT] = false; 310 Cntrlr[LLEFT] = false;} 311 312 if(joystate.stick[0].axis[1] >= .3) 313 Cntrlr[DOWN] = true; 314 else if(joystate.stick[0].axis[1] <= -.3) 315 Cntrlr[LUP] = true; 316 else if (joystate.stick[0].axis[1] >= -.3 && joystate.stick[0].axis[1] <= .3 && Cntrlr[LDOWN] || Cntrlr[LUP]) 317 {Cntrlr[LDOWN] = false; 318 Cntrlr[LUP] = false;} 319 320 if(joystate.stick[1].axis[0] > 0 || joystate.stick[1].axis[0] < 0 || joystate.stick[1].axis[1] < 0 || joystate.stick[1].axis[1] > 0) 321 Cntrlr[RT] = true; 322 else if (joystate.stick[1].axis[0] == 0 && joystate.stick[1].axis[1] == 0 && Cntrlr[RT]) 323 {Cntrlr[RT] = false;} 324 325 if(joystate.button[0]) 326 Cntrlr[A] = true; 327 else if(!joystate.button[0] && Cntrlr[A]) 328 Cntrlr[A] = false; 329 if (joystate.button[6]) 330 {keys[SELECT] = true; 331 done = true;} 332 } 333 334 if(draw && al_is_event_queue_empty(event_queue)) 335 { 336 draw = false; 337 //draw to buffer 338 drawBackground(bg); 339 drawPlanet(Planets, NUM_PLANETS); 340 drawBullet(bullets, NUM_BULLETS); 341 drawPulse(pulseAttack); 342 drawEnemy(enemy, NUM_ENEMIES); 343 drawPlayer(p1); 344 al_draw_textf(font18, al_map_rgb(0,255,255), 10, 10, ALLEGRO_ALIGN_LEFT, "FPS: %d XP: %d/%d Level: %d player: %f,%f", framesPS, p1.expCur, p1.expNeeded, p1.level, p1.x, p1.y); 345 drawHealth(p1); 346 if(gameOver) 347 al_draw_textf(font18, al_map_rgb(0,255,255), width/2, height/2, ALLEGRO_ALIGN_CENTRE, "Game Over."); 348 al_flip_display(); 349 350 //clear buffer 351 al_clear_to_color(al_map_rgb(0,0,0)); 352 353 framesCur++; 354 } 355 356} 357 358al_destroy_display(display); 359al_destroy_bitmap(bg.pic); 360for(int i = 0; i < NUM_ENEMIES; i++) 361{al_destroy_bitmap(enemy[i].pic);} 362al_destroy_bitmap(pulseAttack.pic); 363al_destroy_bitmap(p1.pic); 364al_destroy_bitmap(Planets[0].pic); 365al_destroy_bitmap(Planets[1].pic); 366al_destroy_timer(LPS); 367al_destroy_font(font18); 368al_destroy_event_queue(event_queue); 369 370 371return 0; 372}

Arthur Kalliokoski
Second in Command
February 2005
avatar

I don't really see anything that stands out as being wrong. Although 100 enemies might be a problem depending on how big the sprites are.

I tried compiling it myself to try it out (using some random images to substitute) but it lacks "objects.h" and I don't see the code to load the images.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

pkrcel
Member #14,001
February 2012

Kudos on going the Mike Geig route, his video tutorials are good.

On the other hand, we're talking performance problems here, thus it's very hard find the reason if we can't tag a common situation in which the slowdown happens.

Does the slowdown (which I guess means diminishing FPS) seems random, or is it at timed intervals or maybe when more than (say) 10 comets are on the screen?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Mishtiff
Member #15,776
October 2014

The enemies sprites are 64x64, that get resized to a random number between 40 and 60.
yeah I understand pkcrel, and I've had a horrible time going over it and I cannot understand what I did wrong, unless its some form of bad coding that I have not learned yet.

Objects.h is a seperate header file and contains the following:

#SelectExpand
1//Object IDs 2 3enum IDS{PLAYER, BULLET, ENEMY, IMAGE}; 4 5//Our Player 6struct Player1 7{ 8 int ID; 9 ALLEGRO_BITMAP *pic; 10 int picdelay; 11 int picNum; 12 int picDir; 13 double x; 14 double y; 15 int speed; 16 float health; 17 double angle; 18 int mana; 19 int boundx; 20 int boundy; 21 int expCur; 22 int expNeeded; 23 int level; 24 bool Movement[4]; 25}; 26 27 28struct Bullet 29{ 30 int ID; 31 float x; 32 float y; 33 float destx; 34 float desty; 35 float velx; 36 float vely; 37 float distance; 38 bool live; 39 int speed; 40}; 41 42 43struct Pulse 44{ 45 int ID; 46 ALLEGRO_BITMAP *pic; 47 int picdelay; 48 int picNum; 49 float x; 50 float y; 51 float destx; 52 float desty; 53 float angle; 54 float velx; 55 float vely; 56 float distance; 57 float timer; 58 bool live; 59 int spacing; 60}; 61 62 63struct Enemies 64{ 65 int ID; 66 ALLEGRO_BITMAP *pic; 67 int size; 68 int x; 69 int y; 70 int health; 71 bool live; 72 int speed; 73 int boundx; 74 int boundy; 75 int expGranted; 76}; 77 78 79struct Image 80{ 81 ALLEGRO_BITMAP *pic; 82 int ID; 83 int size; 84 int x; 85 int y; 86 int speed; 87 88}; 89 90struct Background 91{ 92 int ID; 93 ALLEGRO_BITMAP *pic; 94 int width; 95 int height; 96 int placement; 97};

Thomas Fjellstrom
Member #476
June 2000
avatar

Try seeing if there is anything running on your computer that likes to steal a lot of cpu time (virus scanner?).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Arthur Kalliokoski
Second in Command
February 2005
avatar

It still won't compile because it's missing stuff like initBackground() etc.

There aren't any prototypes or definitions for what a Background is, nor a header file to do those things. Maybe your program is garbling things as a result, due to passing incorrect types or something.

[EDIT]

Try seeing if there is anything running on your computer that likes to steal a lot of cpu time (virus scanner?).

Yeah, when I asked how much cpu in Resource Meter I meant how much processor utilization, not how much memory it took.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Mishtiff
Member #15,776
October 2014

the final part of my code is posted below. im sorry but i couldnt not figure out how to upload the game so you could just DL it(making things much easier). it kept saying failed at the end of the upload(possibly too large, even being compressed?)

in my resource monitor, it is using 10-14 cpu. my computer is more than able to run the latest games as well.

there is still a part i cannot link, since its too big for the post... you will have to respond for me to post the final bit.

#SelectExpand
1void initPlayer(Player1 &p1) 2{ 3 p1.x = 80; 4 p1.y = height / 2; 5 p1.ID = PLAYER; 6 p1.pic = al_load_bitmap("Spaceship.png"); 7 al_convert_mask_to_alpha(p1.pic, al_map_rgb(255, 0, 255)); 8 p1.picDir = 1; 9 p1.picNum = 4; 10 p1.picdelay = 0; 11 p1.angle = 0; 12 p1.health = 100; 13 p1.mana = 50; 14 p1.speed = 10; 15 p1.expCur = 0; 16 p1.expNeeded = 100; 17 p1.boundx = 40; 18 p1.boundy = 35; 19 p1.level = 0; 20 21} 22void updatePlayer(Player1 &p1) 23{ 24 p1.picdelay++; 25 if(p1.picdelay == 8) 26 {p1.picNum--; p1.picdelay = 0;} 27 if(p1.picNum == 2) {p1.picNum = 4;} 28 p1.angle = atan2((mousey - p1.y), (mousex - p1.x)); 29} 30void movePlayerUp(Player1 &p1) 31{ 32 if(p1.picDir != 0) 33 {p1.picDir = 0;} 34 p1.y -= p1.speed; 35 mousey -= p1.speed; 36 if(p1.y < 0) p1.y = 0; 37} 38void movePlayerDown(Player1 &p1) 39{ 40 if(p1.picDir != 2) 41 {p1.picDir = 2;} 42 p1.y += p1.speed; 43 mousey += p1.speed; 44 if(p1.y + 30 > height) p1.y = height - 30; 45} 46void movePlayerLeft(Player1 &p1) 47{ 48 if(p1.picDir != 1) 49 {p1.picDir = 1;} 50 p1.x -= p1.speed; 51 mousex -= p1.speed; 52 if(p1.x < 0) p1.x = 0; 53} 54void movePlayerRight(Player1 &p1) 55{ 56 if(p1.picDir != 1) 57 {p1.picDir = 1;} 58 p1.x += p1.speed; 59 mousex += p1.speed; 60 if(p1.x + 30 > width) p1.x = width - 30; 61} 62void drawPlayer(Player1 &p1) 63{ 64 ALLEGRO_BITMAP *plchdr = al_create_bitmap(47, 41), *plchdr1 = al_create_bitmap(94, 82), *prev_bmp; 65 prev_bmp = al_get_target_bitmap(); 66 al_set_target_bitmap(plchdr); 67 al_draw_bitmap_region(p1.pic, p1.picNum * 47, p1.picDir * 41, 47, 41, 0, 0, 0); 68 al_set_target_bitmap(plchdr1); 69 al_draw_scaled_bitmap(plchdr, 0, 0, 47, 41, 0, 0, 94, 82, 0); 70 al_set_target_bitmap(prev_bmp); 71 al_draw_rotated_bitmap(plchdr1, 90, 41, p1.x, p1.y, p1.angle, 0); 72 //DEBUGGING: bounding box for player 73 //al_draw_rectangle(p1.x - p1.boundx, p1.y - p1.boundy, p1.x + p1.boundx, p1.y + p1.boundy, al_map_rgb(255,255,255), 4); 74 al_destroy_bitmap(plchdr); 75 al_destroy_bitmap(plchdr1); 76} 77void drawHealth(Player1 &p1) 78{ 79 if(p1.health > 0) 80 { 81 float curHealth = (p1.health * 4) + 20; 82 al_draw_filled_rectangle(20, 50, curHealth, 90, al_map_rgb(255, 0, 0)); 83 } 84 float firstpoint = 20; 85 float secondpoint = 0; 86 for (int i = 1; i < 11; i++) 87 { 88 if(i != 1) 89 firstpoint += 40; 90 secondpoint = firstpoint + 40; 91 al_draw_rectangle(firstpoint, 50, secondpoint, 90, al_map_rgb(255, 50, 50), 1); 92 } 93} 94 95void initBullet(Bullet bullet[], int size) 96{ 97 for(int i = 0; i < size; i++) 98 { 99 bullet[i].ID = BULLET; 100 bullet[i].speed = 15; 101 bullet[i].destx = 0; 102 bullet[i].desty = 0; 103 bullet[i].distance = 0; 104 bullet[i].velx = 0; 105 bullet[i].vely = 0; 106 bullet[i].live = false;} 107 } 108void drawBullet(Bullet bullet[], int size) 109{ 110 for(int i = 0; i < size; i++) 111 { 112 if(bullet[i].live) 113 { 114 al_draw_line(bullet[i].x, bullet[i].y, bullet[i].x + bullet[i].velx, bullet[i].y + bullet[i].vely, al_map_rgb(0, 255, 0), 2); 115 } 116 } 117} 118void fireBullet(Bullet bullet[], int size, Player1 &p1) 119{ 120 for(int i = 0; i < size; i++) 121 { 122 if(!bullet[i].live) 123 { 124 bullet[i].x = p1.x; 125 bullet[i].y = p1.y; 126 bullet[i].destx = mousex; 127 bullet[i].desty = mousey; 128 bullet[i].distance = sqrt((bullet[i].desty - bullet[i].y) * (bullet[i].desty - bullet[i].y) + (bullet[i].destx - bullet[i].x) * (bullet[i].destx - bullet[i].x)); 129 bullet[i].velx = bullet[i].speed * (bullet[i].destx - bullet[i].x) / bullet[i].distance; 130 bullet[i].vely = bullet[i].speed * (bullet[i].desty - bullet[i].y) / bullet[i].distance; 131 bullet[i].live = true; 132 break; 133 } 134 } 135} 136void updateBullet(Bullet bullet[], int size) 137{ 138 for(int i = 0; i < size; i++) 139 { 140 if(bullet[i].live) 141 { 142 bullet[i].x += bullet[i].velx; 143 bullet[i].y += bullet[i].vely; 144 if(bullet[i].x > width || bullet[i].x < 0 || bullet[i].y > height || bullet[i].y < 0) 145 bullet[i].live = false; 146 } 147 } 148} 149void collideBullet(Bullet bullet[], int bsize, Enemies enemy[], int csize, Player1 &p1) 150{ 151 for(int i = 0; i < bsize; i++) 152 { 153 if(bullet[i].live) 154 { 155 for(int j = 0; j < csize; j++) 156 { 157 if(enemy[j].live) 158 { 159 if(bullet[i].x >= (enemy[j].x - enemy[j].boundx) && 160 bullet[i].x <= (enemy[j].x + enemy[j].boundx) && 161 bullet[i].y >= (enemy[j].y - enemy[j].boundy) && 162 bullet[i].y <= (enemy[j].y + enemy[j].boundy)) 163 { 164 bullet[i].live = false; 165 enemy[j].live = false; 166 p1.expCur += enemy[j].size; 167 } 168 } 169 170 } 171 } 172 } 173} 174 175 176void initPulse(Pulse &pulseAttack) 177{ 178 pulseAttack.ID = BULLET; 179 pulseAttack.pic = al_load_bitmap("Pulse.png"); 180 al_convert_mask_to_alpha(pulseAttack.pic, al_map_rgb(255, 0, 255)); 181 pulseAttack.picNum = 0; 182 pulseAttack.picdelay = 0; 183 pulseAttack.timer = 0; 184 pulseAttack.spacing = 100; 185 pulseAttack.destx = 0; 186 pulseAttack.desty = 0; 187 pulseAttack.velx = 0; 188 pulseAttack.vely = 0; 189 pulseAttack.distance = 600; 190 pulseAttack.live = false; 191} 192void drawPulse(Pulse &pulseAttack) 193{ 194 if(pulseAttack.live) 195 { 196 if(pulseAttack.timer < 4) 197 { 198 for(int i = 0; i < pulseAttack.picNum; i++) 199 { 200 ALLEGRO_BITMAP *plchdr = al_create_bitmap(15, 60), *plchdr1 = al_create_bitmap(240, 360), *prev_bmp; 201 prev_bmp = al_get_target_bitmap(); 202 al_set_target_bitmap(plchdr); 203 al_draw_bitmap_region(pulseAttack.pic, i * 15, 0, 15, 60, 0, 0, 0); 204 al_set_target_bitmap(plchdr1); 205 al_draw_scaled_bitmap(plchdr, 0, 0, 15, 60, 0, 0, 240, 360, 0); 206 al_set_target_bitmap(prev_bmp); 207 al_draw_rotated_bitmap(plchdr1, 80, 190, pulseAttack.x + (pulseAttack.velx * (i * 5)), pulseAttack.y + (pulseAttack.vely * (i * 5)), pulseAttack.angle, 0); 208 //DEBUGGING: bounding box for pulse 209 //al_draw_rectangle(pulseAttack.x, pulseAttack.y, pulseAttack.x, pulseAttack.y, al_map_rgb(255,255,255), 4); 210 al_destroy_bitmap(plchdr); 211 al_destroy_bitmap(plchdr1); 212 } 213 } 214 else 215 { 216 for(int i = 0; i < pulseAttack.picNum; i++) 217 { 218 if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8) 219 continue; 220 ALLEGRO_BITMAP *plchdr = al_create_bitmap(15, 60), *plchdr1 = al_create_bitmap(240, 360), *prev_bmp; 221 prev_bmp = al_get_target_bitmap(); 222 al_set_target_bitmap(plchdr); 223 al_draw_bitmap_region(pulseAttack.pic, i * 15, 0, 15, 60, 0, 0, 0); 224 al_set_target_bitmap(plchdr1); 225 al_draw_scaled_bitmap(plchdr, 0, 0, 15, 60, 0, 0, 240, 360, 0); 226 al_set_target_bitmap(prev_bmp); 227 al_draw_rotated_bitmap(plchdr1, 80, 190, pulseAttack.x + (pulseAttack.velx * (i * 5)), pulseAttack.y + (pulseAttack.vely * (i * 5)), pulseAttack.angle, 0); 228 //DEBUGGING: bounding box for pulse 229 //al_draw_rectangle(pulseAttack.x, pulseAttack.y, pulseAttack.x, pulseAttack.y, al_map_rgb(255,255,255), 4); 230 al_destroy_bitmap(plchdr); 231 al_destroy_bitmap(plchdr1); 232 } 233 234 } 235 } 236} 237void firePulse(Pulse &pulseAttack, Player1 &p1) 238{ 239 if(!pulseAttack.live) 240 { 241 pulseAttack.x = p1.x; 242 pulseAttack.y = p1.y; 243 pulseAttack.destx = mousex; 244 pulseAttack.desty = mousey; 245 pulseAttack.velx = pulseAttack.spacing * (pulseAttack.destx - pulseAttack.x) / pulseAttack.distance; 246 pulseAttack.vely = pulseAttack.spacing * (pulseAttack.desty - pulseAttack.y) / pulseAttack.distance; 247 pulseAttack.angle = atan2((mousey - p1.y), (mousex - p1.x)); 248 pulseAttack.timer = 0; 249 pulseAttack.picNum = 0; 250 pulseAttack.live = true; 251 } 252} 253void updatePulse(Pulse &pulseAttack, Player1 &p1) 254{ 255 if(pulseAttack.live) 256 { 257 pulseAttack.picdelay++; 258 pulseAttack.destx = mousex; 259 pulseAttack.desty = mousey; 260 pulseAttack.velx = pulseAttack.spacing * (pulseAttack.destx - pulseAttack.x) / pulseAttack.distance; 261 pulseAttack.vely = pulseAttack.spacing * (pulseAttack.desty - pulseAttack.y) / pulseAttack.distance; 262 pulseAttack.x = p1.x; 263 pulseAttack.y = p1.y; 264 pulseAttack.angle = atan2((mousey - p1.y), (mousex - p1.x)); 265 if(pulseAttack.picdelay == 2) 266 { 267 pulseAttack.picNum++; 268 pulseAttack.picdelay = 0; 269 pulseAttack.timer++; 270 } 271 if(pulseAttack.timer >= 8) 272 pulseAttack.live = false; 273 } 274} 275void collidePulse(Pulse &pulseAttack, Enemies enemy[], int csize, Player1 &p1) 276{ 277 if(pulseAttack.live) 278 { 279 for(int j = 0; j < csize; j++) 280 { 281 if(enemy[j].live) 282 { 283 if(pulseAttack.x >= (enemy[j].x - enemy[j].boundx) && 284 pulseAttack.x <= (enemy[j].x + enemy[j].boundx) && 285 pulseAttack.y >= (enemy[j].y - enemy[j].boundy) && 286 pulseAttack.y <= (enemy[j].y + enemy[j].boundy)) 287 { 288 enemy[j].live = false; 289 p1.expCur += enemy[j].size; 290 } 291 } 292 293 } 294 } 295}

Arthur Kalliokoski
Second in Command
February 2005
avatar

Mishtiff said:

im sorry but i couldnt not figure out how to upload the game so you could just DL it

You could put everything in a zipfile and use the Drop Attachments Here button

{"name":"608968","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ba347dffbacfa2bcb6f72dfbd5795a.png","w":492,"h":274,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ba347dffbacfa2bcb6f72dfbd5795a"}608968

and it'll show up next to your name as a paperclip, which you could then put in a link with Formatting Help if you wanted.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Mishtiff
Member #15,776
October 2014

there we go, i had to remove some things that weren't necessary. :) (file was too big before to upload i suppose)

ah crap i should have mentioned that the current game only works with an attached controller (i use a connected xbox 360 controller). it will not change shooting directions if you have no controller.

however you can still see the problem without being able to play.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I get these warnings, maybe fixing them will fix the problem. Find out how to enable warnings for your compiler.

main.cpp:316:79: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   else if(joystate.stick[0].axis[0] >= -.3 && joystate.stick[0].axis[0] <= .3 && Cntrlr[LRIGHT] || Cntrlr[LLEFT])
                                                                               ^
main.cpp:324:80: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   else if (joystate.stick[0].axis[1] >= -.3 && joystate.stick[0].axis[1] <= .3 && Cntrlr[LDOWN] || Cntrlr[LUP])
                                                                                ^
main.cpp: In function 'void drawPulse(Pulse&)':
main.cpp:599:16: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8)
                ^
main.cpp:599:88: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8)
                                                                                        ^
main.cpp:599:124: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if(i == 0 && pulseAttack.timer >= 5 || i == 1 && pulseAttack.timer >= 6 || i == 2 && pulseAttack.timer >= 7 || i == 3 && pulseAttack.timer >= 8)
                                                                                                                            ^
main.cpp: In function 'void initEnemy(Enemies*, int)':
main.cpp:688:13: warning: statement has no effect [-Wunused-value]
   enemy[i].x;
             ^
main.cpp:689:13: warning: statement has no effect [-Wunused-value]
   enemy[i].y;
             ^
main.cpp: In function 'int main()':
main.cpp:338:15: warning: array subscript is above array bounds [-Warray-bounds]
   {keys[SELECT] = true;

I also see that player.cpp is 0 bytes? Is that a mistake?

Also, allegro.log is 2.8 megs, probably leave that out.

I see there's a Project1.exe in debug, but my virtual XP machine says it's not a valid Win32 application?

Right now I'm getting a crash in initPulse because the case of the image and the string in the program don't match (linux case sensitive).

I'll play with it a bit more, this is just an update.

[EDIT]

I finally got it to run, it hovers around 58-59 fps, and I don't see any choppiness, but it certainly does look... odd.

{"name":"608970","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/9\/d99b5c5b1dc6274e768323eaf57a8f1a.png","w":1440,"h":900,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/9\/d99b5c5b1dc6274e768323eaf57a8f1a"}608970

[EDIT2]

Compiling without debugging info makes it drop to around 45 fps. Uninitialized pointers?

AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Mishtiff
Member #15,776
October 2014

thank you for your response, im going over each thing you have said to see what happens after the fixes. do you have any idea what i am supposed to do for these:

main.cpp:599:124: warning: suggest parentheses around '&&' within '||' [-Wparentheses]

AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.

this is a great point, thank you. i didnt realize it was such a big deal. i will just pass the loaded bitmap after its initialized in the beginning when the function runs.

pkrcel
Member #14,001
February 2012

AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.

Right! you can also load on request only if the bitmap is nonexistant yet.

But, which this kind of thing shouldn't memory usage skyrocket? Correct me if I'm wrong but this should happen also in debug version.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Arthur Kalliokoski
Second in Command
February 2005
avatar

It wants it like this: FIXED so the compiler doesn't complain.

          if(
            ((i == 0) && (pulseAttack.timer >= 5))
            ||
            ((i == 1) && (pulseAttack.timer >= 6))
            ||
            ((i == 2) && (pulseAttack.timer >= 7))
            ||
            ((i == 3) && (pulseAttack.timer >= 8))

            )
            continue;

I think. That's a horribly complicated line. Maybe break it up more so it's easy to understand.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Mishtiff
Member #15,776
October 2014

I also see that player.cpp is 0 bytes? Is that a mistake?

I was going to start working on a player .cpp and .h, but ran into troubles. I have put that aside for now until i understand how to call them easier in my main.cpp file.

It wants it like this: FIXED so the compiler doesn't complain.

if(
((i == 0) && (pulseAttack.timer >= 5))
||
((i == 1) && (pulseAttack.timer >= 6))
||
((i == 2) && (pulseAttack.timer >= 7))
||
((i == 3) && (pulseAttack.timer >= 8))

)
continue;

wow so picky! it is SO much cleaner though. thank you for putting your time into that, ill fix it all soon :)

I will do my best to clean the code up tomorrow when I get up. Thank you for your time tonight. Hopefully you'll be around tomorrow :) sleep well!

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

No, wait, the compiler is still complaining.

It's the ||s and &&s. You want to tell it which is higher precedence... Something like:

if((i == 0 && pulseAttack.timer >= 5)
 || (i == 1 && pulseAttack.timer >= 6)
 || (i == 2 && pulseAttack.timer >= 7)
 || (i == 3 && pulseAttack.timer >= 8))
             continue;

At least I assume that's the logic you are going for.

Looks to be mostly the same as Arthur's but fewer typos[1] :D

References

  1. which might be a first for me ;D

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Arthur Kalliokoski
Second in Command
February 2005
avatar

I don't know if you noticed one of my edits above, the post wasn't old enough to send to top

AHA! I see one bugaboo now, you're loading bitmaps meteor1.png and meteor2.png in startEnemy() and it's in the loop, so you're loading bitmaps forever. You should load bitmaps to an ALLEGRO_BITMAP at initialization and only use the ALLEGRO_BITMAP * from then on.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Thomas Fjellstrom
Member #476
June 2000
avatar

I don't know if you noticed one of my edits above, the post wasn't old enough to send to top

I did. after I hit send :D

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Mishtiff
Member #15,776
October 2014

Hey guys, I have been working for some time now to fix things. Still seems to stutter here and there, but overall its not as bad. Do you know how I could turn on the warnings that you are getting? I am currently using Visual Studio 2012.

I have fixed the bugs that was posted in your comment earlier, and removed the infinite load of bitmaps in the loop. Still having issues, but ill continue to change some of my code up in the mean time to see if it helps.

Trent Gamblin
Member #261
April 2000
avatar

Only had a quick look at the code. I'm pretty sure you're running into the issue on DirectX where every bitmap you draw into (via al_set_target_bitmap) has to get backed up because of the DirectX (mis)feature of losing graphics content sometimes. You have two options to fix it:

1) Use OpenGL (al_set_new_display_flags(ALLEGRO_OPENGL) before al_create_display)
2) Create your bitmaps with the ALLEGRO_NO_PRESERVE_TEXTURE flag (al_set_new_bitmap_flags(ALLEGRO_NO_PRESERVE_TEXTURE))

Go to: