#include <windows.h>
#include "SDL.h"
#include "SDL_image.h"
#define LEFT 1
#define RIGHT 2
#define TOP 3
#define BOTTOM 4
#define MARIO 100
#define LUIGI 101
class Image
{
public:
int x;
int y;
bool Jump;
bool Falling;
bool Down;
SDL_Surface *CharLeft;
SDL_Surface *CharRight;
SDL_Surface *CharDownRight;
SDL_Surface *CharDownLeft;
SDL_Surface *Char;
void FreeImage();
};
void Image::FreeImage()
{
if (Image::CharLeft)
SDL_FreeSurface(Image::CharLeft);
if (Image::CharDownRight)
SDL_FreeSurface(Image::CharDownRight);
if (Image::CharRight)
SDL_FreeSurface(Image::CharRight);
if (Image::CharDownLeft)
SDL_FreeSurface(Image::CharDownLeft);
}
void Draw(SDL_Surface*, int, int);
int DetectCollision(SDL_Surface*, int&, int&,
SDL_Surface*, int&, int&);
bool Quit();
Image SetImage(char theChar);
void Jump(Image&);
SDL_Surface *screen;
int main(int, char**)
{
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 600, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
MessageBox(NULL,
"Error: Cannot setup screen.",
"Error",
MB_OK | MB_ICONERROR);
return 0;
}
int bgX = 0, bgY = 0;
SDL_Surface *bg = IMG_Load("Images/bg.bmp");
if (bg == NULL)
{
MessageBox(NULL,
"Error: Cannot setup screen.",
"Error",
MB_OK | MB_ICONERROR);
return 0;
}
Image Mario = SetImage('m');
if (Mario.CharLeft == NULL ||
Mario.CharRight == NULL ||
Mario.CharDownRight == NULL ||
Mario.Char == NULL ||
Mario.CharDownLeft == NULL)
return 0;
Image Luigi = SetImage('l');
if (Luigi.CharLeft == NULL ||
Luigi.CharRight == NULL ||
Luigi.CharDownRight == NULL ||
Luigi.Char == NULL ||
Luigi.CharDownLeft == NULL)
return 0;
Uint8 *keys;
SDL_Event event;
bool isRunning = true;
Luigi.x += 30;
while (isRunning == true)
{
keys = SDL_GetKeyState(NULL);
if (SDL_PollEvent(&event) == true)
{
if (event.type == SDL_QUIT && Quit())
isRunning = false;
}
if (keys[SDLK_ESCAPE] && Quit())
isRunning = false;
// Mario
if (keys[SDLK_UP])
{
Mario.Jump = true;
}
if (keys[SDLK_DOWN]);
if (keys[SDLK_LEFT] &&
Mario.x >= 0 &&
DetectCollision(Mario.Char,
Mario.x,
Mario.y,
Luigi.Char,
Luigi.x,
Luigi.y) != RIGHT)
{
Mario.x -= 3;
Mario.Char = Mario.CharLeft;
}
if (keys[SDLK_RIGHT] &&
(Mario.x + Mario.Char->w) <= 800 &&
DetectCollision(Mario.Char,
Mario.x,
Mario.y,
Luigi.Char,
Luigi.x,
Luigi.y) != LEFT)
{
Mario.x += 3;
Mario.Char = Mario.CharRight;
}
// Luigi
if (/*keys[SDLK_KP8]*/false)
{
Luigi.Jump = true;
}
if (keys[SDLK_KP5]);
if (/*keys[SDLK_KP4]*/(Luigi.x - Mario.x) > 100 &&
Luigi.x >= 0 &&
DetectCollision(Luigi.Char,
Luigi.x,
Luigi.y,
Mario.Char,
Mario.x,
Mario.y) != RIGHT)
{
Luigi.x -= 3;
Luigi.Char = Luigi.CharLeft;
}
if (/*keys[SDLK_KP6]*/(Mario.x - Luigi.x) > 100 &&
(Luigi.x + Luigi.Char->w) <= 800 &&
DetectCollision(Luigi.Char,
Luigi.x,
Luigi.y,
Mario.Char,
Mario.x,
Mario.y) != LEFT)
{
Luigi.x += 3;
Luigi.Char = Luigi.CharRight;
}
SDL_SetColorKey(Mario.Char, SDL_SRCCOLORKEY, SDL_MapRGB(Mario.Char->format, 255, 255, 255));
SDL_SetColorKey(Luigi.Char, SDL_SRCCOLORKEY, SDL_MapRGB(Luigi.Char->format, 255, 255, 255));
// Clear the screen to black
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
Draw (bg, bgX, bgY);
if (Mario.Jump)
{
Jump(Mario);
}
else
Draw(Mario.Char, Mario.x, Mario.y);
if (Luigi.Jump)
{
Jump(Luigi);
}
else
Draw(Luigi.Char, Luigi.x, Luigi.y);
SDL_Flip(screen);
}
Mario.FreeImage();
Luigi.FreeImage();
SDL_FreeSurface(bg);
SDL_Quit();
return 0;
}
void Draw(SDL_Surface *sprite, int x, int y)
{
SDL_Rect source;
SDL_Rect dest;
source.x = 0;
source.y = 0;
source.w = sprite->w;
source.h = sprite->h;
dest.x = x;
dest.y = y;
dest.w = source.w;
dest.h = source.h;
SDL_BlitSurface(sprite, &source, screen, &dest);
}
int DetectCollision(SDL_Surface *sprite1, int &x1, int &y1,
SDL_Surface *sprite2, int &x2, int &y2)
{
// Width
if ((x1 + sprite1->w) == x2 && (y1 >= (y2 - sprite2->h) && y1 <= (y2 + sprite2->h))) //&& y2 >= y1 && y2 <= (y1 + sprite1->h)))
return LEFT;
if (x1 == (x2 + sprite2->w) && (y1 >= (y2 - sprite2->h) && y1 <= (y2 + sprite2->h))) //&& y2 >= y1 && y2 <= (y1 + sprite1->h)))
return RIGHT;
// Height
if ((y1 + sprite1->h) == y2 && (x1 >= (x2 - sprite2->w) && x1 <= (x2 + sprite2->w))) //&& x2 >= x1 && x2 <= (x1 + sprite1->w)))
return TOP;
if (y1 == (y2 + sprite1->h) && (x1 >= (x2 - sprite2->w) && x1 <= (x2 + sprite2->w))) //&& x2 >= x1 && x2 <= (x1 + sprite1->w)))
return BOTTOM;
return 0;
}
bool Quit()
{
if (MessageBox(NULL,
"Are you sure?",
"Exit?",
MB_YESNO | MB_ICONWARNING) == IDYES)
return true;
else
return false;
}
Image SetImage(char theChar)
{
Image temp;
temp.x = 0;
temp.y = 454;
temp.Jump = false;
temp.Falling = false;
temp.Down = false;
if (theChar == 'm')
{
temp.CharLeft = IMG_Load("Images/Mario_Left.bmp");
temp.CharRight = IMG_Load("Images/Mario_Right.bmp");
temp.CharDownRight = IMG_Load("Images/Mario_Down_Right.bmp");
temp.CharDownLeft = IMG_Load("Images/Mario_Down_Left.bmp");
}
else if (theChar == 'l')
{
temp.CharLeft = IMG_Load("Images/Luigi_Left.bmp");
temp.CharRight = IMG_Load("Images/Luigi_Right.bmp");
temp.CharDownRight = IMG_Load("Images/Luigi_Down_Right.bmp");
temp.CharDownLeft = IMG_Load("Images/Luigi_Down_Left.bmp");
}
if (temp.CharLeft == NULL ||
temp.CharRight == NULL ||
temp.CharDownRight == NULL ||
temp.CharDownLeft == NULL)
{
MessageBox(NULL,
"Error: Cannot setup an image.",
"Error",
MB_OK | MB_ICONERROR);
}
temp.Char = temp.CharRight;
return temp;
}
inline void Jump(Image &Temp)
{
if (Temp.y > 400 && Temp.Falling == false)
{
Temp.y -= 3;
Draw(Temp.Char, Temp.x, Temp.y);
}
else
{
Temp.Falling = true;
Temp.y += 3;
Draw(Temp.Char, Temp.x, Temp.y);
if (Temp.y == 454)
{
Temp.Jump = false;
Temp.Falling = false;
}
}
return;
}
🔒 Locked
My Code
Advertisement
Original Post
Anyone wanna comment on my code? Keep in mind that I'm under 15 and am NOT a pro.
int main(int, char**)
Shouldn't the parameters have names?
SDL_Init(SDL_INIT_VIDEO);
More error checking.
Could you release an executable? Is this suppose to be a game or a demo of some sort?
Nice job in general, making your variables descriptive, etc. Don't use the excuse that you are fifteen, I'm fifteen. Just post your code and take it like a man ;)
Shouldn't the parameters have names?
SDL_Init(SDL_INIT_VIDEO);
More error checking.
Could you release an executable? Is this suppose to be a game or a demo of some sort?
Nice job in general, making your variables descriptive, etc. Don't use the excuse that you are fifteen, I'm fifteen. Just post your code and take it like a man ;)
Not bad for a beginner. I haven't looked over it in detail, but you seem to have done a fine job.
My one suggestion is to make better use of functions (code modularity and encapsulation). You did have a few functions in there other than main, but you could use quite a few more.
A good guideline is to try and keep as much out of main as possible.
For example, you have this section of code:
Notice how all this code is initialization code? That should signal that it can be taken out of the main function and put into an Init() function which will then be called from main.
The same general idea applies everywhere else to main.
With your code very well encapsulated, main might look something like this:
I could take this encapsulation further, but for your intents and purposes this should be fine.
Good job. [smile]
My one suggestion is to make better use of functions (code modularity and encapsulation). You did have a few functions in there other than main, but you could use quite a few more.
A good guideline is to try and keep as much out of main as possible.
For example, you have this section of code:
SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(800, 600, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); if (screen == NULL) { MessageBox(NULL, "Error: Cannot setup screen.", "Error", MB_OK | MB_ICONERROR); return 0; } int bgX = 0, bgY = 0; SDL_Surface *bg = IMG_Load("Images/bg.bmp"); if (bg == NULL) { MessageBox(NULL, "Error: Cannot setup screen.", "Error", MB_OK | MB_ICONERROR); return 0; } Image Mario = SetImage('m'); if (Mario.CharLeft == NULL || Mario.CharRight == NULL || Mario.CharDownRight == NULL || Mario.Char == NULL || Mario.CharDownLeft == NULL) return 0; Image Luigi = SetImage('l'); if (Luigi.CharLeft == NULL || Luigi.CharRight == NULL || Luigi.CharDownRight == NULL || Luigi.Char == NULL || Luigi.CharDownLeft == NULL) return 0;Notice how all this code is initialization code? That should signal that it can be taken out of the main function and put into an Init() function which will then be called from main.
The same general idea applies everywhere else to main.
With your code very well encapsulated, main might look something like this:
int main(int, char**){ InitSDL(); LoadGameResources(); while (isRunning) { UpdateGame(); RenderGame(); } UnloadGameResources(); DeinitSDL();}I could take this encapsulation further, but for your intents and purposes this should be fine.
Good job. [smile]
Thanks! I was thinking of making more functions and all, but I just finished up to one point so far, so I decided to show you. Ender: I'll upload it to geocities. Here:clicky thing!
Uhhh... What do you mean by 'sprite sheets'? And if all the sprites were on one image, how would I know where to start drawing and where to end??? Oh, and all the bools are for the character ducking, jumping, and falling down. I'm sorry if this seems pathetic to you, but I bet you didnt start as a pro either?
Your app is cool. Are you planing on making a game out of this? I think a Mario clone with Luigi following would be cool. Try and make another demo with collisions so that you can’t jump though Luigi’s head and an obstacle and get Luigi to get over it.
Very good keep up the good work.
Very good keep up the good work.
Thanks! I was thinking of doing exactly that... But in the first day or so when I get back to this project I might try to add some more functions and make the main function a littly more readable. About the collisions with the head... I return TOP if your on top of the other image, but for somereason, it doesnt detect it. If i detect collisions in all, it would only detect you from falling down on luigi's sides, not on his head... My code is in there, could you maybe help me out a bit? But other than that once I get the hang with the collsion part I thought I would made a little test box move towards luigi and if the box was close to luigi he would jump over it... Good idea? Oh, and yeah I was aiming to make something out of this little demo.
Another thing I would personally do is to split up the code into separate source files, keeping all the functions of a similar type together e.g. drawing functions, sound functions etc. You can use a header file to "bridge" the global variables between those source files, using the "extern" keyword.
Doing this makes your code far more manageable, especially if it's a large project.
But it doesn't matter too much, for something small like what you're doing, the way you have it is fine. Apart from what Rob said about the enums (it will save you space in your source file, keeping it less cluttered), I think you're on a good track.
Good luck! [smile]
ukdeveloper.
Doing this makes your code far more manageable, especially if it's a large project.
But it doesn't matter too much, for something small like what you're doing, the way you have it is fine. Apart from what Rob said about the enums (it will save you space in your source file, keeping it less cluttered), I think you're on a good track.
Good luck! [smile]
ukdeveloper.
I GREATLY organized my code... Like it? And is it correctly put?
Image.h:
main.cpp:
Image.cpp:
Like it?
Image.h:
#ifndef _IMAGE_#define _IMAGE_class Image{ public: int x; int y; bool Jump; bool Falling; bool Down; bool Done; SDL_Surface *CharLeft; SDL_Surface *CharRight; SDL_Surface *CharDownRight; SDL_Surface *CharDownLeft; SDL_Surface *Char; void FreeImage();};#endifmain.cpp:
#include <windows.h>#include "SDL.h"#include "SDL_image.h"#include "Image.h"#define LEFT 1#define RIGHT 2#define TOP 3#define BOTTOM 4#define MARIO 100#define LUIGI 101void Draw(SDL_Surface*, int, int);int DetectCollision(SDL_Surface*, int&, int&, SDL_Surface*, int&, int&);bool Quit();Image SetImage(char theChar);void Jump(Image&);void UpdateGame(Image&, Image&, bool&);void DrawGame(Image&, Image&, SDL_Surface*);int InitScreen();SDL_Surface *InitBG();Image InitMario();Image InitLuigi();void FreeResources(Image&, Image&);SDL_Surface *screen;int main(int, char**){ if (InitScreen() == 1) return 0; SDL_Surface *bg = InitBG(); if (bg == NULL) return 0; Image Mario = InitMario(); if (Mario.Done == false) return 0; Image Luigi = InitLuigi(); if (Luigi.Done == false) return 0; bool isRunning = true; Luigi.x += 30; while (isRunning == true) { UpdateGame(Mario, Luigi, isRunning); DrawGame(Mario, Luigi, bg); } FreeResources(Mario, Luigi); SDL_FreeSurface(bg); SDL_Quit(); return 0;}void Draw(SDL_Surface *sprite, int x, int y){ SDL_Rect source; SDL_Rect dest; source.x = 0; source.y = 0; source.w = sprite->w; source.h = sprite->h; dest.x = x; dest.y = y; dest.w = source.w; dest.h = source.h; SDL_BlitSurface(sprite, &source, screen, &dest);}int DetectCollision(SDL_Surface *sprite1, int &x1, int &y1, SDL_Surface *sprite2, int &x2, int &y2){ // Width if ((x1 + sprite1->w) == x2 && (y1 >= (y2 - sprite2->h) && y1 <= (y2 + sprite2->h))) //&& y2 >= y1 && y2 <= (y1 + sprite1->h))) return LEFT; if (x1 == (x2 + sprite2->w) && (y1 >= (y2 - sprite2->h) && y1 <= (y2 + sprite2->h))) //&& y2 >= y1 && y2 <= (y1 + sprite1->h))) return RIGHT; // Height if ((y1 + sprite1->h) == y2 && (x1 >= (x2 - sprite2->w) && x1 <= (x2 + sprite2->w))) //&& x2 >= x1 && x2 <= (x1 + sprite1->w))) return TOP; if (y1 == (y2 + sprite1->h) && (x1 >= (x2 - sprite2->w) && x1 <= (x2 + sprite2->w))) //&& x2 >= x1 && x2 <= (x1 + sprite1->w))) return BOTTOM; return 0;}bool Quit(){ if (MessageBox(NULL, "Are you sure?", "Exit?", MB_YESNO | MB_ICONWARNING) == IDYES) return true; else return false;}Image SetImage(char theChar){ Image temp; temp.x = 0; temp.y = 454; temp.Jump = false; temp.Falling = false; temp.Down = false; temp.Done = true; if (theChar == 'm') { temp.CharLeft = IMG_Load("Images/Mario_Left.bmp"); temp.CharRight = IMG_Load("Images/Mario_Right.bmp"); temp.CharDownRight = IMG_Load("Images/Mario_Down_Right.bmp"); temp.CharDownLeft = IMG_Load("Images/Mario_Down_Left.bmp"); } else if (theChar == 'l') { temp.CharLeft = IMG_Load("Images/Luigi_Left.bmp"); temp.CharRight = IMG_Load("Images/Luigi_Right.bmp"); temp.CharDownRight = IMG_Load("Images/Luigi_Down_Right.bmp"); temp.CharDownLeft = IMG_Load("Images/Luigi_Down_Left.bmp"); } if (temp.CharLeft == NULL || temp.CharRight == NULL || temp.CharDownRight == NULL || temp.CharDownLeft == NULL) { MessageBox(NULL, "Error: Cannot setup an image.", "Error", MB_OK | MB_ICONERROR); } temp.Char = temp.CharRight; return temp;}inline void Jump(Image &Temp){ if (Temp.y > 400 && Temp.Falling == false) { Temp.y -= 3; Draw(Temp.Char, Temp.x, Temp.y); } else { Temp.Falling = true; Temp.y += 3; Draw(Temp.Char, Temp.x, Temp.y); if (Temp.y == 454) { Temp.Jump = false; Temp.Falling = false; } } return;}void UpdateGame(Image &Mario, Image &Luigi, bool &isRunning){ Uint8 *keys = SDL_GetKeyState(NULL); SDL_Event event; if (SDL_PollEvent(&event) == true) { if (event.type == SDL_QUIT && Quit()) isRunning = false; } if (keys[SDLK_ESCAPE] && Quit()) isRunning = false; // Mario if (keys[SDLK_UP]) { Mario.Jump = true; } if (keys[SDLK_DOWN]); if (keys[SDLK_LEFT] && Mario.x >= 0 && DetectCollision(Mario.Char, Mario.x, Mario.y, Luigi.Char, Luigi.x, Luigi.y) != RIGHT) { Mario.x -= 3; Mario.Char = Mario.CharLeft; } if (keys[SDLK_RIGHT] && (Mario.x + Mario.Char->w) <= 800 && DetectCollision(Mario.Char, Mario.x, Mario.y, Luigi.Char, Luigi.x, Luigi.y) != LEFT) { Mario.x += 3; Mario.Char = Mario.CharRight; } // Luigi if (/*keys[SDLK_KP8]*/false) { Luigi.Jump = true; } if (keys[SDLK_KP5]); if (/*keys[SDLK_KP4]*/(Luigi.x - Mario.x) > 100 && Luigi.x >= 0 && DetectCollision(Luigi.Char, Luigi.x, Luigi.y, Mario.Char, Mario.x, Mario.y) != RIGHT) { Luigi.x -= 3; Luigi.Char = Luigi.CharLeft; } if (/*keys[SDLK_KP6]*/(Mario.x - Luigi.x) > 100 && (Luigi.x + Luigi.Char->w) <= 800 && DetectCollision(Luigi.Char, Luigi.x, Luigi.y, Mario.Char, Mario.x, Mario.y) != LEFT) { Luigi.x += 3; Luigi.Char = Luigi.CharRight; } SDL_SetColorKey(Mario.Char, SDL_SRCCOLORKEY, SDL_MapRGB(Mario.Char->format, 255, 255, 255)); SDL_SetColorKey(Luigi.Char, SDL_SRCCOLORKEY, SDL_MapRGB(Luigi.Char->format, 255, 255, 255));}void DrawGame(Image &Mario, Image &Luigi, SDL_Surface *bg){ // Clear the screen to black SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); Draw(bg, 0, 0); if (Mario.Jump) { Jump(Mario); } else Draw(Mario.Char, Mario.x, Mario.y); if (Luigi.Jump) { Jump(Luigi); } else Draw(Luigi.Char, Luigi.x, Luigi.y); SDL_Flip(screen);}int InitScreen(){ if (SDL_Init(SDL_INIT_VIDEO) == -1) { MessageBox(NULL, "Error: Cannot setup screen.", "Error", MB_OK | MB_ICONERROR); return 1; } screen = SDL_SetVideoMode(800, 600, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); if (screen == NULL) { MessageBox(NULL, "Error: Cannot setup screen.", "Error", MB_OK | MB_ICONERROR); return 1; } return 0;}SDL_Surface *InitBG(){ SDL_Surface* bg = IMG_Load("Images/bg.bmp"); if (bg == NULL) { MessageBox(NULL, "Error: Cannot setup screen.", "Error", MB_OK | MB_ICONERROR); return NULL; } return bg;}Image InitMario(){ Image Mario = SetImage('m'); if (Mario.CharLeft == NULL || Mario.CharRight == NULL || Mario.CharDownRight == NULL || Mario.Char == NULL || Mario.CharDownLeft == NULL) { Mario.Done = false; MessageBox(NULL, "Error: Could not setup mario.", "Error", MB_OK | MB_ICONERROR); } return Mario;}Image InitLuigi(){ Image Luigi = SetImage('l'); if (Luigi.CharLeft == NULL || Luigi.CharRight == NULL || Luigi.CharDownRight == NULL || Luigi.Char == NULL || Luigi.CharDownLeft == NULL) { Luigi.Done = false; MessageBox(NULL, "Error: Could not setup luigi.", "Error", MB_OK | MB_ICONERROR); } return Luigi;}void FreeResources(Image &Free1, Image &Free2){ Free1.FreeImage(); Free2.FreeImage();}Image.cpp:
#include "SDL.h"#include "Image.h"void Image::FreeImage(){ if (Image::CharLeft) SDL_FreeSurface(Image::CharLeft); if (Image::CharDownRight) SDL_FreeSurface(Image::CharDownRight); if (Image::CharRight) SDL_FreeSurface(Image::CharRight); if (Image::CharDownLeft) SDL_FreeSurface(Image::CharDownLeft);}Like it?
Sprite sheets are usually used to save speed/time/memory on overhead.
It basically works like this:
Lets say that the image itself is 32 x 32. Lets say we have one sprite sheet for it. It would be 32 x 160 pixels (or 160x 32, whatever).
Lets say we wanted to blit the second sprite, which would be whatever animation. Are you familiar with SDL_BlitSurface???
So if wanted to blit the second sprite, and our sprite sheet was 32 pixels in the x, and 160 in the y, we would:
;
You probably have questions, so ask away.
Edit: Fixed the quote tags. I am so dyslexic sometimes.
[Edited by - ender7771 on September 23, 2005 11:49:08 PM]
It basically works like this:
Lets say that the image itself is 32 x 32. Lets say we have one sprite sheet for it. It would be 32 x 160 pixels (or 160x 32, whatever).
Lets say we wanted to blit the second sprite, which would be whatever animation. Are you familiar with SDL_BlitSurface???
Quote:;
From the SDL Docs
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
Description
This performs a fast blit from the source surface to the destination surface.
Only the position is used in the dstrect (the width and height are ignored).
If either srcrect or dstrect are NULL, the entire surface (src or dst) is copied.
The final blit rectangle is saved in dstrect after all clipping is performed (srcrect is not modified).
So if wanted to blit the second sprite, and our sprite sheet was 32 pixels in the x, and 160 in the y, we would:
SDL_Rect srcRect;srcRect.x = 0; // we want the whole xsrcRect.y = 32 * 1; // 32 for how many pixels each is, and 1 for the second image (arrays start at zero).srcRect.h = 32; // heightsrcRect.w = 32; // width// and then later...SDL_BlitSurface(MARIO_Surface, srcRect, screen, destinationRect);You probably have questions, so ask away.
Edit: Fixed the quote tags. I am so dyslexic sometimes.
[Edited by - ender7771 on September 23, 2005 11:49:08 PM]
Quote:
Original post by dudedbz1
Hmm... not really. I'll stick to normal images for now. Maybe later when I get better with SDL I'll do faster techniques.
Thanks for explaining it though.
I don't see the need to worry about using sprite sheets.
Yeah, it'll speed up your program... but you're a beginner, you're games aren't going to be performance intensive, and frankly, I'd think almost all modern computers can handle the performance hit.
Now, concerning your new code organization: much better. However, you could take the modularity even further.
Notice this code:
SDL_Surface *bg = InitBG(); if (bg == NULL) return 0; Image Mario = InitMario(); if (Mario.Done == false) return 0; Image Luigi = InitLuigi(); if (Luigi.Done == false) return 0; bool isRunning = true; Luigi.x += 30;All this code has something in common: it all initializes the game. As such you could put all of it into a single function.
Other than that: much better. [smile]
I would get rid of those #defines for constants if I were use. The problem with #define'd constants are that they have no type checking, no bound checking, and no scope checking. This is a bad thing. Instead, use something like "const int LEFT = 1;". You can use enums too, but personally I've never been a huge fan of enums. [wink]
Quote:
Original post by dudedbz1
Uhhh... What do you mean by 'sprite sheets'? And if all the sprites were on one image, how would I know where to start drawing and where to end??? Oh, and all the bools are for the character ducking, jumping, and falling down. I'm sorry if this seems pathetic to you, but I bet you didnt start as a pro either?
I have a tutorial on sprite sheets here.
Lazy Foo's SDL tutorials
It's tutorial number 6.
You should also convert your surface to the display format when you load them. I cover that in tutorial 2.
[Edited by - Lazy Foo on August 9, 2007 8:11:06 PM]
Thanks you guys. But I just have one problem:
I understand this could be made into a function that initializes this stuff, but how would main() get those resources? I mean wouldnt they have to be global to be initialized by one function? And last question about the same thing: Is it possible to quit the program from another function? Cuz I should quit the program with this kind of checking:
But since I cant quit from other than main()(atleast i think i cant) how can I take the encapsulation further? Thanks for all the help(and new coming help too)!
SDL_Surface *bg = InitBG(); if (bg == NULL) return 0; Image Mario = InitMario(); if (Mario.Done == false) return 0; Image Luigi = InitLuigi(); if (Luigi.Done == false) return 0; bool isRunning = true; Luigi.x += 30;I understand this could be made into a function that initializes this stuff, but how would main() get those resources? I mean wouldnt they have to be global to be initialized by one function? And last question about the same thing: Is it possible to quit the program from another function? Cuz I should quit the program with this kind of checking:
Image Luigi = InitLuigi(); if (Luigi.Done == false) return 0;But since I cant quit from other than main()(atleast i think i cant) how can I take the encapsulation further? Thanks for all the help(and new coming help too)!
Alright, good questions.
One good method is classes. However, I suspect you don't know what those are, so in your case globals would be the way to go.
If you do know what classes are, however, then you're best bet is to probably make a Game class which handles initialization, shutdown, and updating/rendering the game.
A better method is such:
Basically, keep whether the game is running or not in a global in your case. Repeatedly call the update functions only if this boolean is true. If, in the update function, you come across a situation in which the game need be stopped, you set this boolean to false, which causes the program to exit that 'while' loop, ultimately exiting the program.
HTH,
nilkn
Quote:
I understand this could be made into a function that initializes this stuff, but how would main() get those resources?
One good method is classes. However, I suspect you don't know what those are, so in your case globals would be the way to go.
If you do know what classes are, however, then you're best bet is to probably make a Game class which handles initialization, shutdown, and updating/rendering the game.
Quote:
Is it possible to quit the program from another function?
A better method is such:
// all your globals...bool isGameRunning = true;// more globals...void runGame() { while (isGameRunning) { updateGame(); renderGame(); }}void updateGame() { // ... if (someConditionWhichEndsTheGame) isGameRunning = false; // ...}void main() { initGame(); runGame(); shutdownGame();}Basically, keep whether the game is running or not in a global in your case. Repeatedly call the update functions only if this boolean is true. If, in the update function, you come across a situation in which the game need be stopped, you set this boolean to false, which causes the program to exit that 'while' loop, ultimately exiting the program.
HTH,
nilkn
Quote:
Original post by dudedbz1
I know what classes and structs are.. I am using two classes now: my BaseImage class, and my Image class that inherits from BaseImage. What I dont get is how you can use a class to handle initializing, running, and ending a game...(???).
Ahh yes, doh! [grin] I can't believe I missed that! Sorry. [smile]
Alright, do something along these lines:
class game { private: bool running_; Image* mario_; Image* luigi_; // ... void init() { mario_ = new Image(...); // ... } void shutdown() { delete mario_; delete luigi_; // ... } void update(float dt) { ... } void render() { ... } void run() { while (running_) { update(...); render(); } } public: game() : running_(true), mario(NULL), luigi(NULL) { init(); run(); } ~game() { shutdown(); } };void main() { game gameInst = game();}Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement