Breach

Check-in [1d10f47a25]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added some graphics to test the rotating the paddle in a level. the paddle is rotated and displayed corretly. it moves continuously in a circle in 1 degree increments.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1d10f47a253d1c34dfdb466cddd5e6e7b17caf27
User & Date: donnyjward@gmail.com 2010-09-11 20:07:26
Context
2010-09-12
00:59
Paddle::DoMove is an absolute mess. the function must take the relative x and y mouse movement since the last frame (which is accurate to my knowledge, calculated in StatePlaying::HandleInput) and calculate the new angle to display the paddle at for that frame. currently this function fails miserably check-in: 8cfac02cb1 user: donnyjward@gmail.com tags: trunk
2010-09-11
20:07
added some graphics to test the rotating the paddle in a level. the paddle is rotated and displayed corretly. it moves continuously in a circle in 1 degree increments. check-in: 1d10f47a25 user: donnyjward@gmail.com tags: trunk
2010-09-10
07:16
implemented the StateSplash class. setup gameloop and main function to load and unload the game properly. the game currently starts up and shows the splash screen for 60 frames before exiting. no art has been added yet so there is just a black screen, but the program is protected from segfaults due to missing images. next things to do are to add some basic art, implement STATE_PLAYING (jump straight there from splash, skip menus for now), and see if i can get the paddle on screen and rotating to mouse input check-in: 08b33617a4 user: donnyjward@gmail.com tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.

1
2
3
4
5
6
7
8
9
10
CC = g++
CFLAGS =
LIBS = `sdl-config --cflags --libs` -lSDL_mixer
#SOURCES = main.c game.c
OBJECTS =				\
		$(O)/breach.o		\
		$(O)/g_gameobject.o	\
		$(O)/r_graphics.o	\
		$(O)/r_sound.o		\
		$(O)/s_statemachine.o


|







1
2
3
4
5
6
7
8
9
10
CC = g++
CFLAGS =
LIBS = `sdl-config --cflags --libs` -lSDL_mixer -lSDL_gfx
#SOURCES = main.c game.c
OBJECTS =				\
		$(O)/breach.o		\
		$(O)/g_gameobject.o	\
		$(O)/r_graphics.o	\
		$(O)/r_sound.o		\
		$(O)/s_statemachine.o

Changes to breach.cpp.

1
2
3
4

5
6
7
8
9
10
11

12
13
14
15
16
17
18
19





















20
21
22
23
24
25
26
27
28

29
30

31
32
33
34



35
36
37
38
39
40




41
42
43
44
45
46
47
..
94
95
96
97
98
99
100





101
102
103


104
105
106
107





108
109
110
111
112
113
114
115
116
117
118

119
120
#include <iostream>
#include "SDL/SDL.h"
#include "s_statemachine.h"
#include "r_graphics.h"


#include <string>

using namespace std;

static const string fileID = "[breach.cpp]: ";


int DEBUG = 0;
int FULLSCREEN = 0;

//const vars default to static default (which limits the scope to the file its defined)
//	so declare it extern in all files its used
extern const int COLORKEY_R = 78;
extern const int COLORKEY_G = 0;
extern const int COLORKEY_B = 63;






















extern const int SCREEN_WIDTH = 800;
extern const int SCREEN_HEIGHT = 600;
extern const int SCREEN_BITDEPTH = 32;

//how long splash screen shows before moving to the next state
//	(measured frames)
extern const int SPLASH_DURATION = 60;


//target frame rate
extern const int FPS = 30;


//the main screen surface that will be blitted onto
//	and flipped
SDL_Surface * screen = NULL;




//the little icon for the window/taskbar
SDL_Surface * icon = NULL;

//from s_statemachine.cpp
extern StateMachine * gameState;





void BreachLoop();
void ExitProgram();


int main( int argc, char * argv[] )
{
................................................................................
	return 0;
}

void BreachLoop()
{
	s_initStateMachine();
	gameState = new StateSplash();





	
	while ( s_getGameState() != STATE_EXIT )
	{


		gameState->HandleInput();
		gameState->UpdateGame();
		s_changeState();
		gameState->Render();





	}
	
	ExitProgram();
}

void ExitProgram()
{
	if (DEBUG)
		cout << fileID << "preparing to exit...\n";
		
	delete gameState;

	SDL_Quit();
}




>







>





|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
<
<

>

|
>

<
<
<
>
>
>






>
>
>
>







 







>
>
>
>
>



>
>




>
>
>
>
>











>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47



48
49
50
51
52
53



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include "SDL/SDL.h"
#include "s_statemachine.h"
#include "r_graphics.h"
#include "g_gameobject.h"

#include <string>

using namespace std;

static const string fileID = "[breach.cpp]: ";

//defs.h stuff--------------------------------------------------
int DEBUG = 0;
int FULLSCREEN = 0;

//const vars default to static default (which limits the scope to the file its defined)
//	so declare it extern in all files its used
extern const int COLORKEY_R = 0;
extern const int COLORKEY_G = 0;
extern const int COLORKEY_B = 255;

//how long splash screen shows before moving to the next state
//	(measured frames)
extern const int SPLASH_DURATION = 3;

//the main screen surface that will be blitted onto
//	and flipped
SDL_Surface * screen = NULL;

//paddle constants
extern const int PADDLE_TOPLEFTX = 325;
extern const int PADDLE_TOPLEFTY = 225;
extern const int PADDLE_RADIUS = 75;
extern const int PADDLE_ARCDEGREES = 90;
extern const int PADDLE_THICKNESS = 20; //pixels
extern const int PADDLE_CENTERX = 400;
extern const int PADDLE_CENTERY = 300;

extern const int MAX_GAMEOBJECTS = 100;

//end defs.h stuff--------------------------------

extern const int SCREEN_WIDTH = 800;
extern const int SCREEN_HEIGHT = 600;
extern const int SCREEN_BITDEPTH = 32;






//target frame rate
extern const int FPS = 60;
extern const int ONE_SECOND = 1000; //in milliseconds








//the little icon for the window/taskbar
SDL_Surface * icon = NULL;

//from s_statemachine.cpp
extern StateMachine * gameState;

//TEMPORARY
GameObject * thePaddle;


void BreachLoop();
void ExitProgram();


int main( int argc, char * argv[] )
{
................................................................................
	return 0;
}

void BreachLoop()
{
	s_initStateMachine();
	gameState = new StateSplash();
	thePaddle = new Paddle();
	
	//for capping the framerate
	int startTime = 0;
	int endTime = 0;
	
	while ( s_getGameState() != STATE_EXIT )
	{
		startTime = SDL_GetTicks();
		
		gameState->HandleInput();
		gameState->UpdateGame();
		s_changeState();
		gameState->Render();
		
		endTime = SDL_GetTicks() - startTime;
		
		if ( endTime < (ONE_SECOND/FPS) )
			SDL_Delay( (ONE_SECOND/FPS) - endTime );
	}
	
	ExitProgram();
}

void ExitProgram()
{
	if (DEBUG)
		cout << fileID << "preparing to exit...\n";
		
	delete gameState;
	delete thePaddle;
	SDL_Quit();
}

Changes to defs.h.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22












23
24
25
26
27
28
29
30
31
32
33
#define __DEFS_H__

#include "SDL/SDL.h"

extern int DEBUG;
extern int FULLSCREEN;


//const vars default to static default (which limits the scope to the file its defined)
//	so declare it extern in all files its used
extern const int COLORKEY_R;
extern const int COLORKEY_G;
extern const int COLORKEY_B;

//how long splash screen shows before moving to the next state
//	(in seconds)
extern const int SPLASH_DURATION;

//the main screen surface that will be blitted onto
//	and flipped
extern SDL_Surface * screen;













typedef enum objectType_e
{
	//
} objectType_t;

typedef enum itemType_e
{
	//
} itemType_t;








<













>
>
>
>
>
>
>
>
>
>
>
>



|







2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define __DEFS_H__

#include "SDL/SDL.h"

extern int DEBUG;
extern int FULLSCREEN;


//const vars default to static default (which limits the scope to the file its defined)
//	so declare it extern in all files its used
extern const int COLORKEY_R;
extern const int COLORKEY_G;
extern const int COLORKEY_B;

//how long splash screen shows before moving to the next state
//	(in seconds)
extern const int SPLASH_DURATION;

//the main screen surface that will be blitted onto
//	and flipped
extern SDL_Surface * screen;

//paddle constants
extern const int PADDLE_TOPLEFTX;
extern const int PADDLE_TOPLEFTY;
extern const int PADDLE_RADIUS;
extern const int PADDLE_ARCDEGREES;
extern const int PADDLE_THICKNESS; //pixels
extern const int PADDLE_CENTERX;
extern const int PADDLE_CENTERY;

extern const int MAX_GAMEOBJECTS;


typedef enum objectType_e
{
	OBJECT_PADDLE
} objectType_t;

typedef enum itemType_e
{
	//
} itemType_t;

Changes to g_gameobject.cpp.

4
5
6
7
8
9
10
11




12
13
14
15
16
17
18
..
45
46
47
48
49
50
51
52




53
54
55
56
57
58
59
..
90
91
92
93
94
95
96
97




98
99
100
101
102
103
104
...
125
126
127
128
129
130
131
132




133
134





135
136






137

























































138
139
140
141
142
143





#include <iostream>
#include <string>

using namespace std;

static string fileID = "[g_gameobject.cpp]: ";

//gameobject functions




void GameObject::GetLoc(int & xloc, int & yloc)
{
	xloc = x;
	yloc = y;
}

void GameObject::GetCenter(int & xloc, int & yloc)
................................................................................
//right now only used by paddle, declared in paddle subclass
void GameObject::FixAngle()
{
	while (angle >= 360)
		angle -= 360;
}
*/
//item functions




Item::Item()
{

}

void Item::Draw()
{
................................................................................
*/

Item::~Item()
{

}

//block functions




Block::Block()
{

}
	
void Block::Draw()
{
................................................................................
{

}

//void FixAngle();
//bool DoMove();

//ball functions





//wallsection functions






//paddle functions

































































void Paddle::FixAngle()
{
	while (angle >= 360)
		angle -= 360;
}












|
>
>
>
>







 







|
>
>
>
>







 







|
>
>
>
>







 







|
>
>
>
>

<
>
>
>
>
>

<
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>
>
>
>
>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
137
138
139
140
141
142
143
144
145
146
147
148
149

150
151
152
153
154
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <string>

using namespace std;

static string fileID = "[g_gameobject.cpp]: ";

/************************************************
*                                               *
*	GameObject                              *
*                                               *
************************************************/
void GameObject::GetLoc(int & xloc, int & yloc)
{
	xloc = x;
	yloc = y;
}

void GameObject::GetCenter(int & xloc, int & yloc)
................................................................................
//right now only used by paddle, declared in paddle subclass
void GameObject::FixAngle()
{
	while (angle >= 360)
		angle -= 360;
}
*/
/************************************************
*                                               *
*	Item                                    *
*                                               *
************************************************/
Item::Item()
{

}

void Item::Draw()
{
................................................................................
*/

Item::~Item()
{

}

/************************************************
*                                               *
*	Block                                   *
*                                               *
************************************************/
Block::Block()
{

}
	
void Block::Draw()
{
................................................................................
{

}

//void FixAngle();
//bool DoMove();

/************************************************
*                                               *
*	Ball                                    *
*                                               *
************************************************/


/************************************************
*                                               *
*	Wall Section                            *
*                                               *
************************************************/


/************************************************
*                                               *
*	Paddle                                  *
*                                               *
************************************************/
Paddle::Paddle()
{
	if (DEBUG)
		cout << fileID << "paddle created\n";
		
	angle = 0;
	//change this to point to a preloaded image of paddle with correct facing (prerendering done during load screeN?)
	image = r_loadImage("./graphics/paddle1-315deg.bmp");
	
	if (image == NULL && DEBUG)
		cout << fileID << "load paddle image failed\n";
}
		
Paddle::~Paddle()
{
	//do nothing because the paddle images are part
	//	of an external array which will all be cleared
	//	when gameplay ends, the surface pointer in the
	//	paddle class merely points to one of these images,
	//	it does no actual loading
}

void Paddle::Draw()
{
	SDL_Surface * rotated = NULL;
	
	angle += 1;
	rotated = r_rotateImage(image, angle);
	
	if ( !r_blitSurface(((screen->w - rotated->w)/2), ((screen->h - rotated->h)/2), rotated, screen) && DEBUG )
		cout << fileID << "blit paddle image failed\n";

	if (DEBUG)
		cout << fileID << "paddle current angle = " << angle << "\n";
}

void Paddle::OnCollide(GameObject * otherObject)
{

}

//void GetLoc(int & xloc, int & yloc);
void Paddle::GetCenter(int & xloc, int & yloc)
{
	//xloc = PADDLE_TOPLEFTX + PADDLE_RADIUS;
	//yloc = PADDLE_TOPLEFTY + PADDLE_RADIUS;
	
	xloc = PADDLE_CENTERX;
	yloc = PADDLE_CENTERY;
}

//void SetLoc(int & xloc, int & yloc);
objectType_t Paddle::GetObjectType()
{
	return OBJECT_PADDLE;
}
		
//int GetSpeed();
//int GetAngle();

void Paddle::FixAngle()
{
	while (angle >= 360)
		angle -= 360;
}

bool Paddle::DoMove()
{

}

Changes to g_gameobject.h.

1
2
3
4
5
6
7
8
9
10
11
12
..
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
...
126
127
128
129
130
131
132


133
134
135
136
137
138
139
#ifndef __G_GAMEOBJECT_H__
#define __G_GAMEOBJECT_H__

#include "defs.h"


class GameObject
{
	public:
		virtual void Draw() =0;
		virtual void OnCollide(GameObject * otherObject) =0;
		void GetLoc(int & xloc, int & yloc);
................................................................................
		objectType_t GetObjectType();
		
		virtual int GetSpeed();
		virtual int GetAngle();
		
		virtual ~GameObject() { };
		//FixAngle only needed by paddle
		//void FixAngle(); //possibly make this virtual and only include in classes that will need it
		bool DoMove();
		
	private:
		//current location
		int x;
		int y;
		
		int width;
................................................................................
		void FixAngle();
		bool DoMove();
		
	private:
		int angle;
		int mouseLastX;
		int mouseLastY;


};

class WallSection : public GameObject
{
	public:
		WallSection();
		




|







 







|
|







 







>
>







1
2
3
4
5
6
7
8
9
10
11
12
..
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef __G_GAMEOBJECT_H__
#define __G_GAMEOBJECT_H__

#include "defs.h"
#include "r_graphics.h"

class GameObject
{
	public:
		virtual void Draw() =0;
		virtual void OnCollide(GameObject * otherObject) =0;
		void GetLoc(int & xloc, int & yloc);
................................................................................
		objectType_t GetObjectType();
		
		virtual int GetSpeed();
		virtual int GetAngle();
		
		virtual ~GameObject() { };
		//FixAngle only needed by paddle
		virtual void FixAngle() =0; //possibly make this virtual and only include in classes that will need it
		virtual bool DoMove() =0;
		
	private:
		//current location
		int x;
		int y;
		
		int width;
................................................................................
		void FixAngle();
		bool DoMove();
		
	private:
		int angle;
		int mouseLastX;
		int mouseLastY;
		SDL_Surface * image;
		SDL_Rect dest;
};

class WallSection : public GameObject
{
	public:
		WallSection();
		

Added graphics/background_level1.bmp.

cannot compute difference between binary files

Added graphics/background_menu.bmp.

cannot compute difference between binary files

Added graphics/background_splash.bmp.

cannot compute difference between binary files

Added graphics/paddle1-315deg.bmp.

cannot compute difference between binary files

Changes to r_graphics.cpp.

52
53
54
55
56
57
58


























	offset.y = y;
	
	if ( SDL_BlitSurface(source, clip, dest, &offset) < 0 ) //<0 = fail
		return false;
	else
		return true;
}

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	offset.y = y;
	
	if ( SDL_BlitSurface(source, clip, dest, &offset) < 0 ) //<0 = fail
		return false;
	else
		return true;
}

SDL_Surface * r_rotateImage(SDL_Surface * source, double angle)
{
	SDL_Surface * rotatedPic = NULL;
	SDL_Rect dest2;
	
	rotatedPic = rotozoomSurface (source, angle, 1, SMOOTHING_OFF);
	//rotatedPic = rotozoomSurfaceXY (source, angle, 1.0, 1.0, SMOOTHING_OFF);
	
	Uint32 colorkey;
	colorkey = SDL_MapRGB(rotatedPic->format, COLORKEY_R, COLORKEY_G, COLORKEY_B);
	SDL_SetColorKey(rotatedPic, SDL_SRCCOLORKEY, colorkey);
	
	dest2.x = (screen->w - rotatedPic->w)/2;
	dest2.y = (screen->h - rotatedPic->h)/2;
	dest2.w = rotatedPic->w;
	dest2.h = rotatedPic->h;
	
//	SDL_BlitSurface(rotatedPic, NULL, screen, &dest2);
	//r_blitSurface(PADDLE_TOPLEFTX, PADDLE_TOPLEFTY, rotatedPic, screen, &dest2);
	
	if (rotatedPic == NULL && DEBUG)
		cout << fileID << "failed to rotate image\n";
		
	return rotatedPic;
}

Changes to r_graphics.h.

1
2
3
4



5
6
7
8
9
10
11
12
13
14
15











16
#ifndef __R_GRAPHICS_H__
#define __R_GRAPHICS_H__

#include "SDL/SDL.h"




//loads a bmp file and returns the SDL_Surface
SDL_Surface * r_loadImage(const char * filename);

//Pastes a surface onto the screen with location from top left corner
//	and optionally only a partial section of the surface (clip).
//Does not auto free source surface.
//x and y had default parameters of 0, but in c++ all parameters
//	default parameters must be default as well, so I just left it out
bool r_blitSurface(int x, int y, SDL_Surface * source, SDL_Surface * dest, SDL_Rect * clip = NULL);












#endif




>
>
>











>
>
>
>
>
>
>
>
>
>
>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef __R_GRAPHICS_H__
#define __R_GRAPHICS_H__

#include "SDL/SDL.h"

//mainly for rotating images (just the paddle really)
#include "SDL/SDL_rotozoom.h"

//loads a bmp file and returns the SDL_Surface
SDL_Surface * r_loadImage(const char * filename);

//Pastes a surface onto the screen with location from top left corner
//	and optionally only a partial section of the surface (clip).
//Does not auto free source surface.
//x and y had default parameters of 0, but in c++ all parameters
//	default parameters must be default as well, so I just left it out
bool r_blitSurface(int x, int y, SDL_Surface * source, SDL_Surface * dest, SDL_Rect * clip = NULL);

//wrapper function to keep SDL_rotozoom.h contained in
//	r_graphics.h and not all over the place
//antialiasing is done automatically since all image rotating
//	will be prerendered and stored in memory, so there
//	isnt an issue of doing it realtime since apparently
//	SDL_gfx sucks realtime
//angle is the angle the image will be set to, now how much to
//	increment, so if angle stays at 10, the original image will stay
//	rotated 10 degrees, it wont rotate 10 
SDL_Surface * r_rotateImage(SDL_Surface * source, double angle);

#endif

Changes to s_statemachine.cpp.

9
10
11
12
13
14
15








16
17
18
19
20
21
22
..
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
387
388
389
390
391
392
393


394




395
396
397
398

399


400
401
402
403

404







405
406
407
408
409
410
411
412
413



414




415
416
417
418
419
420
static string fileID = "[s_statemachine.cpp]: ";

gameState_t currentStateID = STATE_NULL;
gameState_t nextStateID = STATE_NULL;

StateMachine * gameState;









void s_initStateMachine()
{
	currentStateID = STATE_SPLASH;
}

gameState_t s_getGameState()
{
................................................................................
*                                               *
*	STATE_SPLASH                            *
*                                               *
************************************************/
StateSplash::StateSplash()
{
	if (DEBUG)
		cout << fileID << "splash screen activated\n";
		
	background = r_loadImage("./graphics/background_splash.bmp");
	
	if (background == NULL && DEBUG)
		cout << fileID << "load background image failed\n";
		
	frame = 0;
................................................................................
}

StateSplash::~StateSplash()
{
	SDL_FreeSurface(background);
	
	if (DEBUG)
		cout << fileID << "splash screen deactivated\n";
}

void StateSplash::HandleInput()
{
	SDL_Event event;
	
	while ( SDL_PollEvent(&event) ) //returns 1 until no more eventz
	{
		switch (event.type)
		{
			case SDL_QUIT:
				s_setNextState(STATE_EXIT);
				break;
................................................................................
		}
	}
}

void StateSplash::UpdateGame()
{
	if (frame >= SPLASH_DURATION)
		s_setNextState(STATE_EXIT);
	else
	{
		cout << fileID << "frame = " << frame << "\n";
		frame++;
	}
}

................................................................................
/************************************************
*                                               *
*	STATE_PLAYING                           *
*                                               *
************************************************/
StatePlaying::StatePlaying()
{







}

StatePlaying::~StatePlaying()
{




}

void StatePlaying::HandleInput()
{









}

void StatePlaying::UpdateGame()
{

}

void StatePlaying::Render()
{








}

//STATE_EXIT
//STATE_NULL









>
>
>
>
>
>
>
>







 







|







 







|




<
<







 







|







 







>
>
|
>
>
>
>




>
|
>
>




>
|
>
>
>
>
>
>
>









>
>
>
|
>
>
>
>






9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
113
114
115
116
117
118
119
120
121
122
123
124


125
126
127
128
129
130
131
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
static string fileID = "[s_statemachine.cpp]: ";

gameState_t currentStateID = STATE_NULL;
gameState_t nextStateID = STATE_NULL;

StateMachine * gameState;

//TEMPOARY
extern GameObject * thePaddle;

//used by the various HandleInput functions, declared
//	up here so it does not continuously go in and
//	out of scope every frame
SDL_Event event;

void s_initStateMachine()
{
	currentStateID = STATE_SPLASH;
}

gameState_t s_getGameState()
{
................................................................................
*                                               *
*	STATE_SPLASH                            *
*                                               *
************************************************/
StateSplash::StateSplash()
{
	if (DEBUG)
		cout << fileID << "StateSplash activated\n";
		
	background = r_loadImage("./graphics/background_splash.bmp");
	
	if (background == NULL && DEBUG)
		cout << fileID << "load background image failed\n";
		
	frame = 0;
................................................................................
}

StateSplash::~StateSplash()
{
	SDL_FreeSurface(background);
	
	if (DEBUG)
		cout << fileID << "StateSplash deactivated\n";
}

void StateSplash::HandleInput()
{


	while ( SDL_PollEvent(&event) ) //returns 1 until no more eventz
	{
		switch (event.type)
		{
			case SDL_QUIT:
				s_setNextState(STATE_EXIT);
				break;
................................................................................
		}
	}
}

void StateSplash::UpdateGame()
{
	if (frame >= SPLASH_DURATION)
		s_setNextState(STATE_PLAYING);
	else
	{
		cout << fileID << "frame = " << frame << "\n";
		frame++;
	}
}

................................................................................
/************************************************
*                                               *
*	STATE_PLAYING                           *
*                                               *
************************************************/
StatePlaying::StatePlaying()
{
	if (DEBUG)
		cout << fileID << "StatePlaying activated\n";
		
	background = r_loadImage("./graphics/background_level1.bmp");
	
	if (background == NULL && DEBUG)
		cout << fileID << "load background image failed\n";
}

StatePlaying::~StatePlaying()
{
	SDL_FreeSurface(background);
	
	if (DEBUG)
		cout << fileID << "StatePlaying deactivated\n";
}

void StatePlaying::HandleInput()
{
	while ( SDL_PollEvent(&event) )
	{
		switch (event.type)
		{
			case SDL_QUIT:
				s_setNextState(STATE_EXIT);
				break;
		}
	}
}

void StatePlaying::UpdateGame()
{

}

void StatePlaying::Render()
{
	//blit background image
	if ( !r_blitSurface(0, 0, background, screen) && DEBUG )
		cout << fileID << "blit background image failed\n";
		
	//blit the paddle with correct facing
	thePaddle->Draw();
	
	SDL_Flip(screen);
}

//STATE_EXIT
//STATE_NULL


Changes to s_statemachine.h.

1
2
3
4
5

6
7
8
9
10
11
12
#ifndef __STATEMACHINE_H__
#define __STATEMACHINE_H__

#include "SDL/SDL.h"
#include "defs.h"


//sets up the initial state of this state machine
void s_initStateMachine();

//used by main loop to fetch currentStateID
gameState_t s_getGameState();






>







1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef __STATEMACHINE_H__
#define __STATEMACHINE_H__

#include "SDL/SDL.h"
#include "defs.h"
#include "g_gameobject.h"

//sets up the initial state of this state machine
void s_initStateMachine();

//used by main loop to fetch currentStateID
gameState_t s_getGameState();