SDL program crash on close - c0000005? - c++

trying to just make a simple program to display the picture loaded into the system. If I type in an image name not found in the directory it returns and error to say it hasn't been found, but for some reason after this when I return 0; inside the main I get a crash.
I cannot find a way to solve it. Any help for what I'm doing wrong?
#include "SDL_Wrapper.h"
#include <iostream>
#include <string>
using namespace std;
class Image {
public:
Image();
~Image();
int loadImage();
int saveImage();
int outputImage();
protected:
int height, width;
string imageName;
SDL_Surface* displayWindow;
SDL_Surface* imageSurface;
SDL_Surface* tempSurface;
};
Image::Image() {
displayWindow = NULL;
imageSurface = NULL;
tempSurface = NULL;
}
Image::~Image() {
}
int Image::saveImage() {
return 0;
}
int Image::outputImage() {
return 0;
}
int Image::loadImage() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
cout << "Error: Did not initialise correctly" << endl;
}
cout << "Enter the image name (and extension) you wish to use - ";
cin >> imageName;
imageSurface = LoadImage(imageName.c_str());
// Check if the image has been loaded correctly
if (imageSurface == NULL) {
imageSurface = NULL;
SDL_Quit();
cout << "Error: Picture not loaded/found" << endl;
return 1;
}
displayWindow = SDL_SetVideoMode(imageSurface->w, imageSurface->h, 32, SDL_SWSURFACE);
if (displayWindow == NULL) {
cout << "Error: displayWindow is blank, halting";
return 1;
}
SDL_WM_SetCaption("Image Preview", NULL);
SDL_BlitSurface(imageSurface, NULL, displayWindow, NULL);
if (SDL_Flip(displayWindow) == -1) {
cout << "Error: displayWindow did not flip correctly";
return 1;
}
SDL_Event event;
bool quit = false;
while (quit == false) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_FreeSurface(imageSurface);
SDL_Quit();
return 0;
}
void test()
{
Image img;
img.loadImage();
}
int main(int argc, char * argv[]) {
test();
return 0;
}
Edit:
Compiler tells me this -
Unhandled exception at 0x776015de in SDL_Display.exe: 0xC0000005: Access violation reading location 0x61d47bce.
In the 'Call Stack' it is on the line
- > SDL_Display.exe!std::_DebugHeapDelete<std::locale::facet>(std::locale::facet * _Ptr) Line 62 + 0xc bytes C++
Edit 2: LoadImage() function -
// Load Image
SDL_Surface* LoadImage(const char* ImageLocation)
{
// Initialize Variables
SDL_Surface *SDL_S_Return = NULL;
// Load Image
SDL_S_Return = IMG_Load(ImageLocation);
// If it hasn't been loaded...
if(SDL_S_Return == NULL)
{
// Send out an Error Message
fprintf(stderr, "Error: %s\n", IMG_GetError());
}
// Return the Surface (Will be NULL if file isn't loaded)
return SDL_S_Return;
}

your code is OK. it runs well and the only problem I can see is:
you did not convert loaded image to display format.
try this function:
SDL_Surface *loadImage(char *imageToLoad)
{
SDL_Surface *rawImage = NULL;
SDL_Surface *fixedImage = NULL;
//load image from file
rawImage = IMG_Load(imageToLoad);
//fix image's depth
fixedImage = SDL_DisplayFormat(rawImage);
//transparenty
Uint32 colorkey = SDL_MapRGB(fixedImage->format,0xff,0,0);
SDL_SetColorKey(fixedImage,SDL_SRCCOLORKEY,colorkey);
//free memory
SDL_FreeSurface(rawImage);
return fixedImage;
}

Related

"Class" type redefinition error C2011 in C++

Here's my header file:
#ifndef SOMETHING_H
#define SOMETHING_H
#include <SDL.h>
#include <stdio.h>
#include <string>
#pragma once
class Something {
public:
//Screen dimension constants
int SCREEN_WIDTHS;
int SCREEN_HEIGHTS;
//Starts up SDL and creates window
bool inits();
//Loads media
bool loadMediases();
//Frees media and shuts down SDL
void closes();
Something(int height);
int mains();
//Loads individual image
SDL_Surface* loadSurfaces(std::string path);
//The window we'll be rendering to
SDL_Window* gWindows;
//The surface contained by the window
SDL_Surface* gScreenSurfaces;
//The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaceses[5];
//Current displayed image
SDL_Surface* gCurrentSurfaces;
};
#endif
Here's my cpp file:
//Using SDL, standard IO, and strings
#include <SDL.h>
#include <stdio.h>
#include <string>
#include "Something.h"
class Something {
public:
//Screen dimension constants
int SCREEN_WIDTHS = 640;
int SCREEN_HEIGHTS = 480;
//Key press surfaces constants
enum KeyPressSurfaceses
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
//The window we'll be rendering to
SDL_Window* gWindows = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurfaces = NULL;
//The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaceses[KEY_PRESS_SURFACE_TOTAL];
//Current displayed image
SDL_Surface* gCurrentSurfaces = NULL;
Something(int height) {
SCREEN_HEIGHTS = height;
}
bool inits()
{
//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
gWindows = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTHS, SCREEN_HEIGHTS, SDL_WINDOW_SHOWN);
if (gWindows == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get window surface
gScreenSurfaces = SDL_GetWindowSurface(gWindows);
}
}
return success;
}
bool loadMedias()
{
//Loading success flag
bool success = true;
//Load default surface
gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT] = loadSurfaces("resources/images/press.bmp");
if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT] == NULL)
{
printf("Failed to load default image!\n");
success = false;
}
//Load up surface
gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP] = loadSurfaces("resources/images/up.bmp");
if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP] == NULL)
{
printf("Failed to load up image!\n");
success = false;
}
//Load down surface
gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN] = loadSurfaces("resources/images/down.bmp");
if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN] == NULL)
{
printf("Failed to load down image!\n");
success = false;
}
//Load left surface
gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT] = loadSurfaces("resources/images/left.bmp");
if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT] == NULL)
{
printf("Failed to load left image!\n");
success = false;
}
//Load right surface
gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT] = loadSurfaces("resources/images/right.bmp");
if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT] == NULL)
{
printf("Failed to load right image!\n");
success = false;
}
return success;
}
void closes()
{
//Deallocate surfaces
for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
{
SDL_FreeSurface(gKeyPressSurfaceses[i]);
gKeyPressSurfaceses[i] = NULL;
}
//Destroy window
SDL_DestroyWindow(gWindows);
gWindows = NULL;
//Quit SDL subsystems
SDL_Quit();
}
SDL_Surface* loadSurfaces(std::string path)
{
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
return loadedSurface;
}
int mains(int argc, char* args[])
{
//Start up SDL and create window
if (!inits())
{
printf("Failed to initialize!\n");
}
else
{
//Load media
if (!loadMedias())
{
printf("Failed to load media!\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//Set default current surface
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT];
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//User presses a key
else if (e.type == SDL_KEYDOWN)
{
//Select surfaces based on key press
switch (e.key.keysym.sym)
{
case SDLK_UP:
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP];
break;
case SDLK_DOWN:
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN];
break;
case SDLK_LEFT:
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_RIGHT:
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT];
break;
default:
gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
}
//Apply the current image
SDL_BlitSurface(gCurrentSurfaces, NULL, gScreenSurfaces, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindows);
}
}
}
//Free resources and close SDL
closes();
return 0;
}
};
Why does it keep throwing a "Class" type redefinition error? How can I fix this? I've tried everything but I just run into more problems. I saw some posts relating the issue to defining the class twice, but getting rid of the class definition in the cpp and using classname::functionname just causes more errors.
I know this is a stupid problem to have, I'm a beginning C++ programmer using tutorials and Visual Studio.
In your .cpp file you have completely redeclared class Something. That's not how you do it if you want to just put the implementations of functions in the .cpp file.
The syntax in there should be like:
bool Something::inits()
{
// implementation ...
}
bool Something::loadMedias()
{
// implementation ...
}
And so on

SDL 2 blitting BMP File

Currently trying to display a BMP file on a window through SDL 2
This is my main.cpp file:
`#include "pch.h"
#include <iostream>
#include<SDL.h>
using namespace std;
int main(int argc, char* args[])
{
bool run=true;
sdl a;
if (a.init() == false)
{
cout << "SDL not working" << endl;
return 0;
}
else
if (a.screendisplay() == false)
{
cout << "Enable to open the window" << endl;
return 0;
}
else
if (a.loadmedia() == false)
{
cout << "Unable to load the image" << endl << SDL_GetError()<<endl;
return 0;
}
else
a.imageprocessing();
SDL_Delay(2000);
a.quit();
return 0;
}`.
This is my pch.H file,which was mentioned above in the code:
class sdl
{
public:
bool init();
bool screendisplay();
bool quit();
bool loadmedia();
bool imageprocessing();
private:
SDL_Surface *gsurface = NULL;
SDL_Window * Window = NULL;
SDL_Surface * screenSurface = NULL;
};
Pch.cpp file:
#include "pch.h"
using namespace std;
bool sdl::init()
{
bool sucess = true;
if (SDL_Init(SDL_INIT_VIDEO) <0)
{
cout << "SDL not Working properply" << endl;
return false;
}
else return true;
};
bool sdl::screendisplay()
{
bool sucess;
Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
if (Window = NULL)
{
return sucess = false;
}
else
screenSurface= SDL_GetWindowSurface(Window);
return sucess = true;
};
bool sdl::loadmedia()
{
bool success = true;
gsurface = SDL_LoadBMP("asd.bmp");
if (gsurface == NULL)
{
success = false;
}
return success;
};
bool sdl::imageprocessing()
{
bool success = true;
if (SDL_BlitSurface(gsurface, NULL, screenSurface, NULL) < 0)
{
return success = false;
}
else
SDL_UpdateWindowSurface(Window);
SDL_Delay(2000);
return success;
};
bool sdl::quit()
{
bool sucess;
SDL_DestroyWindow(Window);
SDL_Quit();
return sucess = true;
};
When i run this program,the window appears but with no image. What am i doing wrong here?
I think the problem is in your screendisplay() function:
bool sdl::screendisplay()
{
bool sucess;
Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
if (Window = NULL) // <- Right here
{
return sucess = false;
}
else
screenSurface= SDL_GetWindowSurface(Window);
return sucess = true;
};
You're doing an assignment rather than a comparison. You should turn up your compiler warnings so it catches this.

SDL_GetRendererInfo crash

the following program runs until SDL_GetRendererInfo is called (in function RenderInit()), and then stops working before SDL_GetError can do anything. replacing global_renderer with a null SDL_Renderer pointer doesn't cause a crash and the expected sdl error is gotten.
#include <iostream>
#include <list>
#include "SDL.h"
using namespace std;
//global variables:
bool run = true; // whether or not the program should be running
int global_window_width = 50; //width of window in tiles
int global_window_height = 35; //height of window in tiles
SDL_Window* global_window = NULL; //points to primary window
SDL_Renderer* global_renderer = NULL; //points to renderer for main window
SDL_RendererInfo* global_renderer_info = NULL; //points to info about above renderer once it's initialized
SDL_Texture* spritesheet = NULL; //holds the spritesheet
SDL_Event event; //for holding currently in-handling event
//function declarations:
int init(); //initialize SDL
int windowInit(); //initialize window
int renderInit(); //create renderers
int viewInit(); //manages all window, rendering, etc stuff
int loadSpritesheet(); //loads spritesheet
void cleanup(); //free up memory, etc
void dispatchEvent(); //main event handling
//function definitions:
int init()
{
if ( SDL_Init(SDL_INIT_EVERYTHING) !=0 )
{
cout << "could not initialize SDL. " << SDL_GetError();
return 1;
}
return 0;
}
int windowInit()
{
global_window = SDL_CreateWindow("roguelike",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
global_window_width * 10,
global_window_height * 10,
SDL_WINDOW_SHOWN);
if(global_window == NULL)
{
cout << "could create window. " << SDL_GetError();
return 1;
}
return 0;
}
int renderInit()
{
global_renderer = SDL_CreateRenderer(global_window,
-1,
SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED );
if (global_renderer == NULL)
{
cout << "could not create renderer" << SDL_GetError();
return 1;
}
SDL_SetRenderDrawColor(global_renderer,0xFF,0xFF,0xFF,0xFF);
if (SDL_GetRendererInfo(global_renderer, global_renderer_info) != 0)
{
cout << "could not get renderer info" << SDL_GetError();
return 1;
}
return 0;
}
int viewInit()
{
if (windowInit() == 1)
{
return 1;
}
else if(renderInit() == 1)
{
return 1;
}
else if(loadSpritesheet() == 1)
{
return 1;
}
return 0;
}
int loadSpritesheet()
{
SDL_Surface* tempsurf = NULL; //using surface to get image initially, but since surfaces use cpu rendering we switch to textures immediately
tempsurf = SDL_LoadBMP("spritesheet.bmp"); //puts image in the surface
if (tempsurf == NULL)
{
cout << "failed to load spritesheet";
SDL_FreeSurface(tempsurf); //we don't need tempsurf anymore
return 1;
}
spritesheet = SDL_CreateTextureFromSurface(global_renderer, tempsurf);
if (spritesheet == NULL)
{
cout << "failed to create spritesheet texture";
SDL_FreeSurface(tempsurf); //we don't need tempsurf anymore
return 1;
}
SDL_FreeSurface(tempsurf); //we don't need tempsurf anymore
return 0;
}
void cleanup()
{
SDL_DestroyWindow(global_window);
global_window = NULL;
SDL_DestroyRenderer(global_renderer);
global_renderer = NULL;
SDL_DestroyTexture(spritesheet);
spritesheet = NULL;
global_renderer_info = NULL;
SDL_Quit();
}
void dispatchEvent()
{
SDL_PollEvent(&event); //stores current event information
switch(event.type)
{
case SDL_QUIT:
{
run = false;
break;
}
}
}
//classes:
class Layer // each layer holds visuals for a certain subset of what is to be displayed; environment, HUD, menu, etc.
{
Layer(){};
};
class Camera //renders environment, ui, etc in a series of layers
{
int width, height; //in tiles
SDL_Texture* texture_draw; //texture the camera draws to and sends to be displayed
list<Layer*> layer_list; //list of layers to be rendered, back --> front
public:
Camera(int x, int y, SDL_Renderer* renderer): width(x), height(y) //(width, height, renderer for camera to use)
{
texture_draw = SDL_CreateTexture(global_renderer,global_renderer_info->texture_formats[0] , SDL_TEXTUREACCESS_TARGET, 10*width, 10*height);
};
};
//main loop
int main(int argc, char *argv[]) //main function, needed by SDL
{
if(init() == 0)
{
if(viewInit() == 0)
{
while(run)
{
dispatchEvent();
}
}
}
cleanup();
return 0;
}
You're asking SDL_GetRendererInfo to write renderer info to location global_renderer_info points to, which is NULL. Writing to NULL causes segmentation fault. This should be expected behaviour.
Correct code should allocate memory for renderer info (preferably on stack or in global area) and write to that location, e.g.:
SDL_RendererInfo info = {0};
SDL_GetRendererInfo(global_renderer, &info);
// ... use info fields
Fixed by: instead of declaring global_renderer_info as a pointer to an SDL_RendererInfo, I instead declared the object directly and changed the rest of the code accordingly.

Error to pixel manipulation SDL2 c++

i'm trying to create a manual color key with pixel manipulation in SDL2, but when i execute the program, the color key doesn't work, these pixels become white, but not transparent.
I tried to Lock the surface and set the surface blendmode, but doesn't work too.
Code:
// IO includes
#include <iostream>
// String includes
#include <string>
// SDL includes
#include <SDL.h>
#include <SDL_image.h>
// Screen constants
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
using namespace std;
// Window variables
SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
// Image surfaces
SDL_Surface *background = NULL;
SDL_Surface *image = NULL;
// Functions
bool initializeSDL();
SDL_Surface *loadSurfaceFromFile(string path);
void SetColorKey(SDL_Surface *surface, Uint32 color);
bool LoadFiles();
void FreeMemory();
// SDL_main
int main(int argc, char* argv[]){
// Try to initialize and load files
if(initializeSDL() == false){
FreeMemory();
return -1;
}
if(LoadFiles() == false){
FreeMemory();
return -1;
}
// Blit images
SDL_BlitSurface(background, NULL, windowSurface, NULL);
SDL_BlitSurface(image, NULL, windowSurface, new SDL_Rect(70, 277, image->w, image->h));
// Update screen
SDL_UpdateWindowSurface(window);
// Loop
SDL_Event ev;
while(true){
if(SDL_PollEvent(&ev) && ev.type == SDL_QUIT) break;
}
return 0;
}
bool initializeSDL(){
// Try to initialize the video
if( SDL_Init(SDL_INIT_VIDEO) < 0 ){
// Print error
cout << "Error to initialize the video: " << SDL_GetError() << endl;
return false;
}
// Try to create the window
window = SDL_CreateWindow("Surface", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL){
// Print error
cout << "Error to create the window: " << SDL_GetError() << endl;
return false;
}
// Try to initialize PNG format
int imgFlags = IMG_INIT_PNG;
if(! (IMG_Init(imgFlags) &imgFlags) ) return false;
// Get the window surface
windowSurface = SDL_GetWindowSurface(window);
// If everything is fine, return true
return true;
}
SDL_Surface* loadSurfaceFromFile(string path){
// Create a temporary surface
SDL_Surface *tmpSurface = NULL;
// Create an optimized surface
SDL_Surface *optimizedSurface = NULL;
// Try to load the tmpSurface
tmpSurface = IMG_Load(path.c_str());
if(tmpSurface == NULL){
// Print error
cout << "Error to load the image: " << SDL_GetError() << endl;
return NULL;
}
// Try to optimize the surface
optimizedSurface = SDL_ConvertSurface(tmpSurface, windowSurface->format, NULL);
SDL_FreeSurface(tmpSurface); // Free tmpSurface memory
delete tmpSurface; // Delete the tmpSurface
if(optimizedSurface == NULL){
// Print error
cout << "Error to optimize the image: " << SDL_GetError() << endl;
}
return optimizedSurface;
}
void SetColorKey(SDL_Surface *surface, Uint32 color){
Uint32 *pixels = (Uint32*)surface->pixels;
int pixelCount = (surface->pitch/4) * surface->h;
Uint32 transparent = SDL_MapRGBA(image->format, 255, 255, 255, 0);
for(int i=0;i<pixelCount;i++){
if(pixels[i] == color){
pixels[i] = transparent;
}
}
}
bool LoadFiles(){
// Try to load the background
background = loadSurfaceFromFile("background.bmp");
if(background == NULL) return false;
// Try to load the image
image = loadSurfaceFromFile("image.png");
if(image == NULL) return false;
// Set manual color key to image
SetColorKey(image, SDL_MapRGB(image->format, 0, 255, 255));
return true;
}
void FreeMemory(){
SDL_FreeSurface(background);
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
}

Unable to open Image in C++ SDL

So im trying to render an image. This code worked fine before I refactored into seperate classes but i cannot see the problem. The file path for the image also worked before the refactoring however i have not moved the image so it should still be in the same place.
The error i am getting is "Unable to create texture from surface. Error: Couldn't open Images/text.bmp" so im fairly certain its the line of code below that is messing up.
if (!(gTexture->LoadFromFile("Images/text.bmp")))
{
return false;
}
Here is the LoadFromFile function code
bool Texture2D::LoadFromFile(string path)
{
Free();
SDL_Texture* pTexture = NULL;
SDL_Surface* pSurface = IMG_Load(path.c_str());
if (pSurface != NULL)
{
//SDL_SetColorKey(pSurface, SDL_TRUE, SDL_MapRGB(pSurface->format, 0, 0xFF, 0xFF));
pTexture = SDL_CreateTextureFromSurface(mRenderer, pSurface);
if (pTexture == NULL)
{
cout << "Unable to create texture from surface. Error: " << SDL_GetError() << endl;
}
mWidth = pSurface->w;
mHeight = pSurface->h;
SDL_FreeSurface(pSurface);
}
else
{
cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
}
mTexture = pTexture;
return pTexture != NULL;
}
Below is the Source.cpp file:
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include "Constants.h"
#include "Texture2D.h"
#include "Commons.h"
#include <iostream>
#include <string>
using namespace std;
//Globals
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
Texture2D* gTexture = NULL;
//prototypes
bool InitSDL();
void CloseSDL();
bool Update();
void Render();
int main(int argc, char* args[])
{
if(InitSDL())
{
bool quit = false;
while(!quit)
{
//gTexture->Render(Vector2D(), SDL_FLIP_NONE, 0);
Render();
quit = Update();
}
}
CloseSDL();
return 0;
}
bool InitSDL()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL did not initialise. Error: " << SDL_GetError();
return false;
}
else
{
//ITS ALL GOOD BRO
gWindow = SDL_CreateWindow("Games Engine Creation",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
//DID IT GO?
if (gWindow == NULL)
{
//NUH UH
cout << "Window was not created. Error: " << SDL_GetError();
return false;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer != NULL)
{
//INITIALISE THAT PNG BBY
int imageFlags = IMG_INIT_PNG;
if (!(IMG_Init(imageFlags) & imageFlags))
{
cout << "SDL_Image could not initialise. Error: " << IMG_GetError();
return false;
}
}
else
{
cout << "Renderer could not be initialised. Error: " << SDL_GetError();
return false;
}
gTexture = new Texture2D(gRenderer);
if (!(gTexture->LoadFromFile("Images/text.bmp")))
{
return false;
}
return true;
}
}
void CloseSDL()
{
SDL_DestroyWindow(gWindow);
gWindow = NULL;
delete gTexture;
gTexture = NULL;
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
IMG_Quit();
SDL_Quit();
}
bool Update()
{
SDL_Event e;
SDL_PollEvent(&e);
switch(e.type)
{
case SDL_QUIT:
return true;
break;
case SDL_KEYUP:
switch(e.key.keysym.sym)
{
case SDLK_q:
return true;
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
return true;
break;
}
return false;
}
void Render()
{
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
gTexture->Render(Vector2D(), SDL_FLIP_NONE, 0);
//SDL_Rect renderLocation = { 0, 0, mWidth, mHeight };
//SDL_RenderCopyEx(mRenderer, mTexture, NULL, &renderLocation, 0, NULL, SDL_FLIP_NONE);
SDL_RenderPresent(gRenderer);
}
A simple way to start debugging is to place the image in the same folder with the executable and show the path as ./image.bmp to see if something is wrong with the path.