|
What the hell is wrong with this?!? |
Don Freeman
Member #5,110
October 2004
|
This is driving me crazy! I can't seem to figure out why the ammo is not being changed! It is staying at 0. Here is the code: Shooter.h: 1//////////////////////////////////////////////////////////////////////////////////////////////////
2// <Shooter.h>
3//////////////////////////////////////////////////////////////////////////////////////////////////
4#ifndef SHOOTER_H
5#define SHOOTER_H
6//////////////////////////////////////////////////////////////////////////////////////////////////
7#include <time.h>
8#include <math.h>
9#include <allegro.h>
10#include <winalleg.h>
11#include <winsock.h>
12//////////////////////////////////////////////////////////////////////////////////////////////////
13#define MAX_AMMO 100
14#define PI 3.1415926535897932384626433832795f
15//////////////////////////////////////////////////////////////////////////////////////////////////
16int InitSYS( void );
17float GetDegrees( float radians );
18float GetRadians( float degrees );
19//////////////////////////////////////////////////////////////////////////////////////////////////
20class Ammo;
21class Weapon;
22class Player;
23//////////////////////////////////////////////////////////////////////////////////////////////////
24class Ammo
25{
26public:
27 Ammo();
28 ~Ammo();
29 void Update( Player & );
30 void Display( BITMAP *pBmp );
31 float x, y;
32 float xVel, yVel;
33 float angle;
34 float vel;
35 int damage;
36 bool bActive, bVisible, bFired;
37};
38//////////////////////////////////////////////////////////////////////////////////////////////////
39class Weapon
40{
41public:
42 Weapon();
43 ~Weapon();
44 void Update( Player & );
45 void Display( BITMAP *pBmp );
46 void Fire( void );
47 int maxAmmo;
48 int curAmmo;
49 Ammo ammo[MAX_AMMO];
50};
51//////////////////////////////////////////////////////////////////////////////////////////////////
52class Player
53{
54public:
55 Player();
56 ~Player();
57 void Update( void );
58 void Display( BITMAP *pBmp );
59 void Fire( void );
60 float x, y;
61 float xVel, yVel;
62 float angle;
63 float vel;
64 Weapon *pWeapon;
65 BITMAP *pBmp;
66};
67//////////////////////////////////////////////////////////////////////////////////////////////////
68#endif // SHOOTER_H
69//////////////////////////////////////////////////////////////////////////////////////////////////
Shooter.cpp: 1//////////////////////////////////////////////////////////////////////////////////////////////////
2// <Shooter.cpp>
3//////////////////////////////////////////////////////////////////////////////////////////////////
4#include "Shooter.h"
5//////////////////////////////////////////////////////////////////////////////////////////////////
6int InitSYS( void )
7{
8 allegro_init();
9 set_color_depth(32);
10 set_gfx_mode(GFX_DIRECTX_WIN,400,400,0,0);
11 set_color_conversion(COLORCONV_TOTAL);
12 install_timer();
13 install_keyboard();
14 install_mouse();
15 return 0;
16}
17//////////////////////////////////////////////////////////////////////////////////////////////////
18float GetDegrees( float radians )
19{
20 return (radians * 180 / PI);
21}
22//////////////////////////////////////////////////////////////////////////////////////////////////
23float GetRadians( float degrees )
24{
25 return (degrees * PI / 180);
26}
27//////////////////////////////////////////////////////////////////////////////////////////////////
28Ammo::Ammo()
29{
30 x = 0.0f;
31 y = 0.0f;
32 vel = 0.5f;
33 xVel = 0.0f;
34 yVel = 0.0f;
35 angle = 0.0f;
36 damage = 1;
37 bActive = false;
38 bVisible = false;
39 bFired = false;
40}
41Ammo::~Ammo()
42{
43}
44void Ammo::Update( Player & player )
45{
46 //if ( !bFired )
47 //{
48 // x = player.x;
49 // y = player.y;
50 // xVel = 0.0f;
51 // yVel = 0.0f;
52 // angle = player.angle;
53 // return;
54 //}
55 //if ( !bActive )
56 // return;
57 //xVel = vel * cos( GetRadians( angle ) );
58 //yVel = vel * sin( GetRadians( angle ) );
59 //x += xVel;
60 //y += yVel;
61 x = 50;
62 y = 50;
63}
64void Ammo::Display( BITMAP *pDest )
65{
66 //if ( !bVisible )
67 // return;
68 circlefill(pDest,x,y,20,makecol(255,255,255));
69}
70//////////////////////////////////////////////////////////////////////////////////////////////////
71Weapon::Weapon()
72{
73 maxAmmo = MAX_AMMO-1;
74 for ( int i = 0; i < maxAmmo; i++ )
75 {
76 //ammo<i>;
77 }
78 curAmmo = maxAmmo;
79}
80Weapon::~Weapon()
81{
82 for ( int i = 0; i < maxAmmo; i++ )
83 {
84 //if ( pAmmo<i> )
85 // delete pAmmo<i>;
86 //pAmmo<i> = NULL;
87 }
88}
89void Weapon::Update( Player & player )
90{
91 for ( int i = 0; i < maxAmmo; i++ )
92 {
93 //if ( pAmmo<i> )
94 //{
95 ammo<i>.Update(player);
96 //}
97 }
98}
99void Weapon::Display( BITMAP *pDest )
100{
101 if ( !pDest )
102 return;
103 for ( int i = 0; i < maxAmmo; i++ )
104 {
105 //if ( pAmmo<i> )
106 //{
107 ammo<i>.Display(pDest);
108 //}
109 }
110}
111void Weapon::Fire( void )
112{
113 if ( curAmmo <= 0 )
114 {
115 curAmmo = 0;
116 return;
117 }
118 if ( curAmmo > maxAmmo )
119 {
120 curAmmo = maxAmmo;
121 return;
122 }
123 for ( int i = 0; i < maxAmmo; i++ )
124 {
125 ammo<i>.bActive = true;
126 ammo<i>.bFired = true;
127 ammo<i>.bVisible = true;
128 }
129 //pAmmo[curAmmo]->bActive = true;
130 //pAmmo[curAmmo]->bFired = true;
131 //pAmmo[curAmmo]->bVisible = true;
132 curAmmo--;
133}
134//////////////////////////////////////////////////////////////////////////////////////////////////
135Player::Player()
136{
137 x = 0.0f;
138 y = 0.0f;
139 vel = 0.0f;
140 xVel = 0.0f;
141 yVel = 0.0f;
142 angle = GetRadians(0.0f);
143 pWeapon = new Weapon;
144 pBmp = NULL;
145}
146Player::~Player()
147{
148 if ( pWeapon )
149 delete pWeapon;
150 pWeapon = NULL;
151 if ( pBmp )
152 destroy_bitmap(pBmp);
153 pBmp = NULL;
154}
155void Player::Display( BITMAP *pDest )
156{
157 if ( !pBmp )
158 return;
159 if ( !pDest )
160 return;
161 rotate_sprite(pDest,pBmp,x+pBmp->w/2,y+pBmp->h/2,ftofix(GetRadians(angle*40.743665)));
162}
163void Player::Update( void )
164{
165 xVel = vel * cos( GetRadians( angle ) );
166 yVel = vel * sin( GetRadians( angle ) );
167 x += xVel;
168 y += yVel;
169 pWeapon->ammo[0].x = x;
170 pWeapon->ammo[0].y = y;
171 //pWeapon->Update(*this);
172 xVel=0;
173 yVel=0;
174}
175void Player::Fire( void )
176{
177 pWeapon->Fire();
178}
179//////////////////////////////////////////////////////////////////////////////////////////////////
Main.cpp: 1//////////////////////////////////////////////////////////////////////////////////////////////////
2// <Main.cpp>
3//////////////////////////////////////////////////////////////////////////////////////////////////
4#include "Shooter.h"
5//////////////////////////////////////////////////////////////////////////////////////////////////
6int main( void )
7{
8 //////////////////////////////////////////////////////////////////////////////////////////////
9 InitSYS();
10 //////////////////////////////////////////////////////////////////////////////////////////////
11 BITMAP *pBmp = create_bitmap(SCREEN_W,SCREEN_H);
12 clear_bitmap(pBmp);
13 Player *pLocalPlayer = new Player;
14 Player *pRemotePlayer = new Player;
15 //////////////////////////////////////////////////////////////////////////////////////////////
16 pLocalPlayer->x = (SCREEN_W / 2) - 10;
17 pLocalPlayer->y = SCREEN_H - 100;
18 pLocalPlayer->angle = GetRadians(0.0f);
19 pLocalPlayer->pBmp = load_bitmap("ShipA.bmp",NULL);
20 //////////////////////////////////////////////////////////////////////////////////////////////
21 pRemotePlayer->x = (SCREEN_W / 2) - 10;
22 pRemotePlayer->y = 100;
23 pRemotePlayer->angle = 0.0f;//GetRadians(180.0f);
24 pRemotePlayer->pBmp = load_bitmap("ShipB.bmp",NULL);
25 //////////////////////////////////////////////////////////////////////////////////////////////
26 while ( true )
27 {
28 if ( keypressed() )
29 {
30 if ( key[KEY_ESC] )
31 {
32 clear_keybuf();
33 break;
34 }
35 if ( key[KEY_LEFT] )
36 {
37 pLocalPlayer->angle -= 0.5f;
38 }
39 if ( key[KEY_RIGHT] )
40 {
41 pLocalPlayer->angle += 0.5f;
42 }
43 if ( key[KEY_UP] )
44 {
45 pLocalPlayer->vel += 0.01f;
46 }
47 if ( key[KEY_DOWN] )
48 {
49 pLocalPlayer->vel -= 0.01f;
50 }
51 if ( key[KEY_SPACE] )
52 {
53 pLocalPlayer->Fire();
54 }
55 }
56 pLocalPlayer->Update();
57 pRemotePlayer->Update();
58 pLocalPlayer->Display(pBmp);
59 pRemotePlayer->Display(pBmp);
60 //pLocalPlayer->pWeapon->ammo[0].x = 50;
61 //pLocalPlayer->pWeapon->ammo[0].y = 50;
62 textprintf(pBmp,font,0, 0,makecol(255,255,255),"Player 1->Ammo: %i", pLocalPlayer->pWeapon->curAmmo);
63 textprintf(pBmp,font,0, 10,makecol(255,255,255),"Player 1->Ammo->x: %d",
64 pLocalPlayer->pWeapon->ammo[0].x);
65 textprintf(pBmp,font,0, 20,makecol(255,255,255),"Player 1->Ammo->y: %d",
66 pLocalPlayer->pWeapon->ammo[0].y);
67 blit(pBmp,screen,0,0,0,0,pBmp->w,pBmp->h);
68 clear_bitmap(pBmp);
69 }
70 //////////////////////////////////////////////////////////////////////////////////////////////
71 delete pLocalPlayer;
72 delete pRemotePlayer;
73 destroy_bitmap(pBmp);
74 return 0;
75 //////////////////////////////////////////////////////////////////////////////////////////////
76}
77END_OF_MAIN()
78//////////////////////////////////////////////////////////////////////////////////////////////////
When I run the program, I see the two ships...like I should. I can move them around...like I should. I can "fire" the weapon because it allows me to see the ammo count going down. ammo[x] should be valid, but will not change! Don't mind all of those pesky // everywhere...still trying to track down the bug. -- |
Michael Faerber
Member #4,800
July 2004
|
I think it's a bit unfair to let us look through all your source. You should play around with your debugger, adding in-code logging messages and reduce the code to the minimum part that causes you problems. AFTER THAT you can post here! And please a bit more specific than "the ammo is not being changed! It is staying at 0." Sorry if I am too harsh. -- |
Niunio
Member #1,975
March 2002
|
And use TRACE to trace the execution ----------------- |
Don Freeman
Member #5,110
October 2004
|
I HAVE tryed to find the bug! I'm not one to just post as soon as I run into problems...I would like to find this on my own, but I can't seem to think of a reason that would make it act the way it is! To be more specific: All seems to work just fine. The Player class contains valid data. The Weapons class contains valid data. The Ammo class contains valid data. It acts as though Weapon->Update(...) and / or Weapon->Fire(...) are not calling Ammo's functions. So, to rule this out, I tryed to just manually set the values in main....still, ammo->x, ammo->y are set to 0...even thought I manually set them to 50 with: pLocalPlayer->pWeapon->ammo[0]->x = 50.0f; pLocalPlayer->pWeapon->ammo[0]->y = 50.0f; This may have been cut out of the above code...as I am still trying to solve this. I keep changing the code around to minimize the possible flaws...but I can't seem to think of WHY it is acting this way anyway! I mean, if ammo[0] is valid, pWeapon is valid, and pLocalPlayer is valid....then calling: pLocalPlayer->pWeapon->ammo[0]->x = 50.0f should actually set ammo[0]->x to 50.0f! But when I go to retreive the values of these...I get 0! -- |
Audric
Member #907
January 2001
|
Quote: the ammo is not being changed! It is staying at 0
Quote: it allows me to see the ammo count going down. You seem to be confused. Quote: ammo[x] should be valid, but will not change! staying at 0, going down, or no change? One advice: set MAX_AMMO at 1, and fix until it works with one bullet. The most I can comment is:
|
Don Freeman
Member #5,110
October 2004
|
Yeah...there is ALOT of debugging crap in that code! And I am just using one of Ammo class...pLocalPlayer->pWeapon->ammo[0] to be exact! Quote:
Quote: Quote: Quote: No...I'm not confused by WHAT is not changing! ammo count is in weapon...which IS changing! The problem I am having is with ammo. I ran the debugger...which sucks (MSVC 6), and I see that ammo[?]->x and ammo[?]->y are jumping all over the place...but in the when the code is ran...it is just 0. Confusing. I may just scrap this and start again...looks like some evil little voodoo spirit has corrupted this beast! It's just frustrating that it should NOT be acting like this....even the debugger shows that all data structures are VALID! -- |
Fladimir da Gorf
Member #1,565
October 2001
|
Quote: I see that ammo[?]->x and ammo[?]->y are jumping all over the place...but in the when the code is ran...it is just 0. Most likely memory corruption in your code, a bit too typical in pointer-oriented C code. Maybe using C++, STL and proper interfaces would help? OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
Audric
Member #907
January 2001
|
Quote: there is ALOT of debugging crap in that code
Well, I can't tell what is INTENDED to be debugging crap from what is the cause of your bug - honestly, it's truly hard to guess what you voluntarily cut, from what was already missing |
Don Freeman
Member #5,110
October 2004
|
Ok...I've got this worked out now! Thanks all that helped... Sorry about all the crappy code I posted...was hard to tell what was suppose to be there, since I was trying to pick away at areas I thought could cause the problem. Was something stupid and quickly overlooked...even in the debugger. It was showing int values instead of float...that's why it looked so confusing. The ammo's Display(...) function was never being called... Anyway, if anyone is interested in seeing this demo so far...it's attached. It is a windows app. It includes the GFX and SFX that it needs to play. Has some bg music, some cheesy fire sounds, engine humm....blah blah blah. It is going to be my test engine for real time network play...the source is changing drastically, but is coming along quite nicely though! -- |
|