problems with my struct? or with my masked blitting?
MSGleader

hello, ive ran into a rather wierd problem and i think it might either have to do with my structs, or the way i am blitting my bitmaps to the buffer, but i had the game working fine so far until i added a new struct into my header file. I added the struct for a "bigball", but when i go to blit the ball to the screen it messes up the coordinates for my second player, and for some reason the ball and the second player both go to the same position... if anyone has the time, id appreciate it if they would take a look at my code... :-/

this is my header file named header.h

#SelectExpand
1#ifndef _HEADER_H 2#define _HEADER_H 3 4#include <allegro.h> 5 6struct tagPlayers 7{ 8 float x,y; 9 float speed; 10 int grabx,graby; 11 int frame,row; 12 int anim_timer,anim_delay; 13 int alive; 14}player[1]; 15 16struct tagBigBalls 17{ 18 float x,y; 19 float y_vel; 20 float speed; 21 int alive; 22}bigball[1]; 23 24#endif

And this is my main.cpp

#SelectExpand
1#include "header.h" 2 3void init(); 4void deinit(); 5void clear_buffer(BITMAP *buffer,BITMAP *backround); 6void player1_code(SAMPLE *die); 7void player1_anim(); 8void player2_code(); 9void player1_anim(); 10void set_variables(); 11void draw_balls(BITMAP *buffer,BITMAP *big_ball,BITMAP *medium_ball,BITMAP *smallball); 12void draw_buffer(BITMAP *buffer,BITMAP *player1,BITMAP *player2); 13 14void clear_buffer(BITMAP *buffer,BITMAP *backround){ 15 blit(backround,buffer,0,0,0,0,SCREEN_W,SCREEN_H); 16 } 17 18void player1_code(SAMPLE *die){ 19 //input on keyboard 20 player[0].row = 2; 21 if(key[KEY_RIGHT] && player[0].alive == 1){ 22 player[0].row = 0; 23 player[0].x += player[0].speed; 24 } 25 if(key[KEY_LEFT] && player[0].alive == 1){ 26 player[0].row = 1; 27 player[0].x -= player[0].speed; 28 } 29 if(player[0].alive == 0){ 30 player[0].row = 3; 31 } 32 //left and right boundry 33 if(player[0].x-8 < 0){ 34 player[0].x = 8; 35 } 36 if(player[0].x+8 > 640){ 37 player[0].x = 632; 38 } 39 if(key[KEY_F]){ 40 if(!player[0].alive == 0){ 41 play_sample(die,255,128,1500,FALSE); 42 } 43 player[0].alive = 0; 44 } 45 if(key[KEY_G]){ 46 player[0].alive = 1; 47 } 48 } 49 50void player1_anim(){ 51 player[0].anim_timer--; 52 if(player[0].anim_timer <= 0){ 53 player[0].frame++; 54 player[0].anim_timer = player[0].anim_delay; 55 } 56 if(player[0].frame > 2){ 57 player[0].frame = 0; 58 } 59 } 60 61void player2_code(){ 62 //input on keyboard 63 player[1].row = 2; 64 if(key[KEY_D] && player[1].alive == 1){ 65 player[1].row = 0; 66 player[1].x += player[1].speed; 67 } 68 if(key[KEY_A] && player[1].alive == 1){ 69 player[1].row = 1; 70 player[1].x -= player[1].speed; 71 } 72 if(player[1].alive == 0){ 73 player[1].row = 3; 74 } 75 //left and right boundry 76 if(player[1].x-8 < 0){ 77 player[1].x = 8; 78 } 79 if(player[1].x+8 > 640){ 80 player[1].x = 632; 81 } 82 if(key[KEY_F]){ 83 player[1].alive = 0; 84 } 85 if(key[KEY_G]){ 86 player[1].alive = 1; 87 } 88 } 89 90void player2_anim(){ 91 player[1].anim_timer--; 92 if(player[1].anim_timer <= 0){ 93 player[1].frame++; 94 player[1].anim_timer = player[1].anim_delay; 95 } 96 if(player[1].frame > 2){ 97 player[1].frame = 0; 98 } 99 } 100 101void set_variables(){ 102 //set game variables 103 player[0].x = 490; 104 player[0].y = 448; 105 player[0].speed = 1; 106 player[0].frame = 0; 107 player[0].row = 0; 108 player[0].anim_delay = 8; 109 player[0].anim_timer = player[0].anim_delay; 110 player[0].alive = 1; 111 player[1].x = 150; 112 player[1].y = 448; 113 player[1].speed = 1; 114 player[1].frame = 0; 115 player[1].row = 0; 116 player[1].anim_delay = 8; 117 player[1].anim_timer = player[1].anim_delay; 118 player[1].alive = 1; 119 bigball[0].x = 50; 120 bigball[0].y = 50; 121 } 122 123void draw_balls(BITMAP *buffer,BITMAP *big_ball,BITMAP *medium_ball,BITMAP *smallball){ 124 //stuff here 125 masked_blit(big_ball,buffer,0,0,bigball[0].x-32,bigball[0].y-32,64,64); 126 } 127 128void draw_buffer(BITMAP *buffer,BITMAP *player1,BITMAP *player2){ 129 //player 1 bitmap values 130 player[0].grabx = 16*player[0].frame; 131 player[0].graby = 16*player[0].row; 132 //player 2 bitmap values 133 player[1].grabx = 16*player[1].frame; 134 player[1].graby = 16*player[1].row; 135 //player 1 to buffer 136 masked_blit(player1,buffer,player[0].grabx,player[0].graby,player[0].x-8,player[0].y-8,16,16); 137 //player 2 to buffer 138 masked_blit(player2,buffer,player[1].grabx,player[1].graby,player[1].x-8,player[1].y-8,16,16); 139 //buffer to the screen 140 blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); 141 } 142 143int main() { 144 init(); 145 146 //define bitmaps 147 BITMAP *buffer; 148 buffer = create_bitmap(640,480); 149 BITMAP *player1; 150 player1 = load_bitmap("bitmaps/player1.bmp",NULL); 151 BITMAP *player2; 152 player2 = load_bitmap("bitmaps/player2.bmp",NULL); 153 BITMAP *backround; 154 backround = load_bitmap("bitmaps/backround.bmp",NULL); 155 BITMAP *big_ball; 156 big_ball = load_bitmap("bitmaps/bigball.bmp",NULL); 157 BITMAP *medium_ball; 158 medium_ball = load_bitmap("bitmaps/mediumball.bmp",NULL); 159 BITMAP *small_ball; 160 small_ball = load_bitmap("bitmaps/smallball.bmp",NULL); 161 SAMPLE *die; 162 die = load_sample("sounds/splat.wav"); 163 set_variables(); 164 165 while (!key[KEY_ESC]) { 166 clear_buffer(buffer,backround); 167 player1_code(die); 168 player1_anim(); 169 player2_code(); 170 player2_anim(); 171 draw_balls(buffer,big_ball,medium_ball,small_ball); 172 draw_buffer(buffer,player1,player2); 173 //slow the game down 174 vsync(); 175 } 176 177 deinit(); 178 //destroy_bitmaps 179 destroy_bitmap(buffer); 180 destroy_bitmap(player1); 181 destroy_bitmap(player2); 182 return 0; 183} 184END_OF_MAIN() 185 186void init() { 187 int depth, res; 188 allegro_init(); 189 depth = desktop_color_depth(); 190 if (depth == 0) depth = 32; 191 set_color_depth(depth); 192 res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); 193 if (res != 0) { 194 allegro_message(allegro_error); 195 exit(-1); 196 } 197 198 install_timer(); 199 install_sound(DIGI_AUTODETECT,MIDI_NONE,""); 200 install_keyboard(); 201 install_mouse(); 202 /* add other initializations here */ 203} 204 205void deinit() { 206 clear_keybuf(); 207 /* add other deinitializations here */ 208}

CosmicR
1#ifndef _HEADER_H
2#define _HEADER_H
3 
4#include <allegro.h>
5 
6struct tagPlayers
7{
8 float x,y;
9 float speed;
10 int grabx,graby;
11 int frame,row;
12 int anim_timer,anim_delay;
13 int alive;
14}player[2]; // change this to 2
15 
16struct tagBigBalls
17{
18 float x,y;
19 float y_vel;
20 float speed;
21 int alive;
22}bigball[1];
23 
24#endif

you need to initilise your players with the total number of elements, which is 2(1+1) as opposed to 1(0+1). stupid I know, but thats how it is.

MSGleader

but player[2] means that there is three players then doesnt it? or do you not use the 0?

CosmicR

yeah, you still address the first one using 0, but you initialise it with the total number of elements, not the final elements address.

MSGleader

ok, so i would put player[1]...
and then when i set the variables or used them later i would use...
player[1].x = 50;
^but player[1].x is really meaning player[0].x in the struct?

or would i just keep everything else the same, and only change the struct player[2];
I'm dumb!
im a bit confused here...:(

EDIT:

DUDE!

nevermind! i got it to work by just changing what you said to in the first reply!
thanx so much! I'm dumb!
now it makes sense to me.
when you have a struct sorta like this

struct tagPlayers
{
     float x,y;
     float alive;
     int frame;
}player[2];

the player[2] at the end means theres gonna be 2 players, player[0] is one and player[1] is two :)

thanx a bunch again

CosmicR

yep - you got it. no probs.

Thread #575186. Printed from Allegro.cc