There's a youtube series online that does C++/SDL, (appears to be dead), and it stops just short of finishing the game (that after this tutorial will be more OOP). Before the last two tutorials, he finished the game (it compiled perfectly), and he was in the process of just organizing it (to make it more engine-like, before the last two tutorials it was just one main.cpp file) when he apparently stopped making videos, so I know the code is good, just not exactly where to put it. The code I am unsure of is in the third code snippet.
//THIS CODE SHOULD BE FINE, INCLUDING IT JUST IN CASE
#include "gameEvents.h"
int main (int argc, char* argv[])
{
gameEvents gameEvents;
//!!!
//THIS CODE IS WHAT I'M HAVING AN ISSUE WITH. I DON'T KNOW EXACTLY WHERE TO PUT ALL THE CODE AFTER THE
// const int SCREEN_BPP = 32; TO MAKE THE ENGINE RUN.
//!!!
#include <SDL.h>
#include <SDL_image.h>
const int SCREEN_HEIGHT = 640;
const int SCREEN_WIDTH = 480;
const int SCREEN_BPP = 32;
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) //this applies the surface to the screen
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
All this code is expected to do is move a 32x32 image across the screen, and set boundaries that prevent the image from going out of the screen. You should be able to hold down a movement key and it'll keep moving.
The main thing I don't know what to do with is the void apply_surface() and the void draw_sprite(). (And possibly the sprite values above them, I was thinking I could just leave them in there).
The code compiles as-is, but it's just a blank screen. =\
This has been bugging me all night, your help is greatly appreciated!
You're getting a blank screen because you're not drawing anything. You shoudn't use global variables, for the moment you can make all those variables members of the gameEvents class, or constants in the Game.cpp file.
Other things:
What is the difference between gameLoad and gameInit and the constructor? You have three different initialisation functions, this is confusing. I suggest you do the initialisation in the constructor.
apply_surface() and draw_sprite() should not flip the screen. There is a bug in draw_sprite, it doesn't correctly set the destination rectangle co-ordinates.
You should handle more than one event every frame, using while(SDL_PollEvent()) rather than an if statement.
This is a personal thing but the formatting of your source is very confusing. Tightly packing the braces like that makes the code dense and hard to read. I think it would benefit enormously from more whitespace.
Here is a simple breakdown of what should go where.
Graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination);
void draw_sprite(int srcX, int srcY, int dstX, int dstY, int width, int height, SDL_Surface* source, SDL_Surface* destination);
#endif
Game.h:
#ifndef GAME_H
#define GAME_H
class Game
{
private:
SDL_Surface *screen;
SDL_Surface *background;
SDL_Surface *sprite;
bool running;
int dstX;
int dstY;
public:
Game();
~Game();
void handleInput();
void draw();
void update();
bool isRunning();
};
#endif
Graphics.cpp
#include "Graphics.h"
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
Note that we need a header file for the graphics function prototypes, because if you #include a function body in more than one file you'd get duplicate definition linker errors. I've renamed gameEvents to "Game" as the older name wasn't really suitable given what the class does.
Something like that will get you started. The next step might be to separate the sprite stuff into a separate class.