Hi.
Currently, I have my SDL project in one file (Source.cpp).
It works fine, its main purpose is to display an image from my hard disk onto the screen, then quit after a few seconds.
SOURCE.CPP
#include "SDL.h"
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP("shaan.bmp");
if (gHelloWorld == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError());
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Load media
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
//Apply the image
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
//Wait two seconds
SDL_Delay(2000);
}
}
//Free resources and close SDL
close();
return 0;
}
Here is the version where I have made a “mySDL” class
- SDLManager.h
- SDLManager.cpp
- Source2.cpp
These three files are meant to do exactly the same thing as Source2.cpp, No image is displayed and no error is given. I believe it has something to do with my usage of pointers/passing pointers to functions/referencing memory addresses.
Can someone help me?
Here is the source code for the 3 files:
SDLMANAGER.CPP
#include “SDLManager.h”
SDLManager::SDLManager()
{
}
SDLManager::~SDLManager()
{
}
bool SDLManager::init(SDL_Window *myWindow, SDL_Surface *mySurface)
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialise! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
myWindow = SDL_CreateWindow("SDL-Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (myWindow == NULL)
{
printf("Window could not be created! SDL-Error: %s\n", SDL_GetError());
success = false;
}
else
{
mySurface = SDL_GetWindowSurface(myWindow);
}
}
return success;
}
bool SDLManager::loadMedia( SDL_Surface *myImage)
{
bool success = true;
myImage = SDL_LoadBMP("shaan.bmp");
if (myImage == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n,", "shaan.bmp", SDL_GetError());
success = false;
}
return success;
}
void SDLManager::close(SDL_Window *myWindow, SDL_Surface *myImage)
{
SDL_FreeSurface(myImage);
myImage = NULL;
SDL_DestroyWindow(myWindow);
myWindow = NULL;
SDL_Quit();
}
SDLMANAGER.H
#pragma once
#include “SDL.h”
#include <stdio.h> //standard io
class SDLManager
{
public:
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDLManager();
~SDLManager();
bool init(SDL_Window *myWindow, SDL_Surface *mySurface);
bool loadMedia(SDL_Surface *myImage);
void close(SDL_Window *myWindow, SDL_Surface *myImage);
};
SOURCE2.CPP
#include "SDLManager.h"
int main(int argc, char* args[])
{
SDLManager *mySDL = new SDLManager();
SDL_Window *myWindow = NULL;
SDL_Surface *mySurface = NULL;
SDL_Surface *myImage = NULL;
if (!mySDL->init(myWindow, mySurface))
{
printf("Failed to initialise!");
//return 0; No need, as return is done in init function.
}
else {
if (!mySDL->loadMedia(myImage))
{
printf("Failed to load media.");
}
else
{
SDL_BlitSurface(myImage, NULL, mySurface, NULL);
SDL_UpdateWindowSurface(myWindow);
SDL_Delay(5000);
}
}
mySDL->close(myWindow, myImage);
return 0;
}