I'm trying to put an imagine on surface.
So it's showed a black screen when I'm load BMP and display bmp
here's my code:
#define SDL_MAIN_HANDLED
#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool good = true;
if(SDL_Init(SDL_INIT_VIDEO)<0){
std::cout<<"ERROR: Failed to initialize SDL " << SDL_GetError()<<"\n";
good = false;
}else{
//Create Window
window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
if (window ==NULL){
std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
good = false;
}else{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
}
}
return good;
}
void close(){
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(window);
window=NULL;
SDL_Quit();
}
bool loadMedia(){
bool ok = true;
gHelloWorld=SDL_LoadBMP("hello_world.bmp");
if(gHelloWorld==NULL){
std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
ok = false;
}else{
std::cout<<"Loaded successfully "<<'\n';
}
return ok;
}
int main(){
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
bool Init = init();
if(Init){
bool loadMed = loadMedia();
if(loadMed){
SDL_Rect area ={30,30,250,250};
SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
SDL_UpdateWindowSurface(window); //
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
close();
return 0;
}
And I'm using Cmake to link lib (dynamic linking)
I tried to write SDL_Rect , and its not work ;/
Your problem is that you are nulling your sufaces in the main function.
Here is a version of your code without that mistake:
#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool good = true;
if(SDL_Init(SDL_INIT_VIDEO)<0){
std::cout<<"ERROR: Failed to initialize SDL " << SDL_GetError()<<"\n";
good = false;
}else{
//Create Window
window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
if (window ==NULL){
std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
good = false;
}else{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
}
}
return good;
}
void close(){
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(window);
window=NULL;
SDL_Quit();
}
bool loadMedia(){
bool ok = true;
gHelloWorld=SDL_LoadBMP("/tmp/image.bmp");
if(gHelloWorld==NULL){
std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
ok = false;
}else{
std::cout<<"Loaded successfully "<<'\n';
}
return ok;
}
int main(){
// your error was nulling your surfaces.
// SDL_Window* window=NULL;
// SDL_Surface* screenSurface=NULL;
bool Init = init();
if(Init){
bool loadMed = loadMedia();
if(loadMed){
SDL_Rect area ={30,30,250,250};
SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
SDL_UpdateWindowSurface(window); //
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
close();
return 0;
}
Also, I recommend that you make your surface updates inside your while loop because somethimes SDL don't render the first surface and you obta a false window.
Here is a good resource for learning SDL: https://lazyfoo.net/tutorials/SDL/
and on my GitHub I have a tool to give you an SDL example: https://github.com/Monsterduty/mktest.
Related
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 is getting me a error when try to convert Surface to Texture. SDL_Surface is correct because it wont return NULL as like the SDL_Texture. Any ideas of what it might be? I already put m_pRenderer as private variable from Game class and also as global variable ,just to test it, both ways didnt work.
The idea of the program is to create a interface environment with Background.png showing off and add clicking buttons , but in this early stages I only need to show the image.
Game header contains class Game{} and the Game.cpp contains the functions.
Game.cpp:
#include"Game.h"
//init,render,update,handleEvents,clean
bool Game::init(const char* title,int xpos,int ypos,int width,int height,int flags){
//attempt to initialize SDL
if(SDL_Init(SDL_INIT_VIDEO)==0){
std::cout<<"SDL init success\n";
//init window
m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);
if(m_pWindow!=0) { //window init success
std::cout<<"Window creation success\n";
int flags=IMG_INIT_JPG|IMG_INIT_PNG;
int initted=IMG_Init(flags);
if((initted&flags) != flags){
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
return false;
//renderer init fail
}
else{
//IMG_INIT was a success
SDL_Surface *m_pSurface;
m_pSurface = IMG_Load("background.png");
if(m_pSurface == NULL){
printf("Error while trying creating surface! SDL_image Error: %s\n",IMG_GetError());
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,m_pSurface);
if(m_pTexture == NULL){
printf("Error while trying creating texture! SDL_image Error: %s\n",IMG_GetError());
}
else{
//success creating the texture
SDL_FreeSurface(m_pSurface);
SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w, &m_sourceRectangle.h);
m_destinationRectangle.x=m_sourceRectangle.x = 0;
m_destinationRectangle.y=m_sourceRectangle.y = 0;
m_destinationRectangle.w=m_sourceRectangle.w;
m_destinationRectangle.h=m_sourceRectangle.h;
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,&m_destinationRectangle);;
SDL_RenderPresent(m_pRenderer);
}
}
}
else{
std::cout<<"Window creation failed\n";
return false;
//window init fail
}
}
else{
std::cout<<"SDL init fail\n";
return false;
//SDL init fail
}
std::cout<<"Initialization was a success\n";
return true; //everything initialized successfully, start the main loop
}
void Game::render(){
SDL_RenderClear(m_pRenderer); //clear the renderer to draw color
SDL_RenderPresent(m_pRenderer); //draw to the screen
}
void Game::handleEvents(){
SDL_Event event;
if(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT: m_bRunning =false; break;
default: break;
}
}
}
void Game::clean(){
std::cout<<"Cleaning game\n";
SDL_DestroyTexture(m_pTexture);
IMG_Quit();
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
Game.h:
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#include<iostream>
class Game{
public:
Game(){};
~Game(){};
//simply set the boolean value to true
bool init(const char*,int,int,int,int,int);
void render();
void update(){};
void handleEvents();
void clean();
bool running(){return m_bRunning;}
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceRectangle,m_destinationRectangle;
bool m_bRunning=true;
};
#endif // GAME_H_INCLUDED
main.cpp:
#include"Game.h"
//our Game object
Game* g_game=0;
int main(int argc, char* argv[]){
g_game = new Game();
g_game->init("Hello SDL",100,100,800,600,SDL_WINDOW_SHOWN);
while(g_game->running()){
g_game->handleEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
m_pRenderer was never created.
Add this after line 16 in file Game.cpp
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);
if(m_pRenderer == nullptr){
printf( "SDL could not create Renderer! SDL_image Error: %s\n", SDL_GetError() );
return false;
}
or
Fixed Game.cpp file
#include "Game.h"
//init,render,update,handleEvents,clean
bool Game::init(const char* title,int xpos,int ypos,int width,int height,int flags){
//attempt to initialize SDL
if(SDL_Init(SDL_INIT_VIDEO)==0){
std::cout<<"SDL init success\n";
//init window
m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);
if(m_pWindow!=0) { //window init success
std::cout<<"Window creation success\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);
if(m_pRenderer == nullptr){
printf( "SDL could not create Renderer! SDL_image Error: %s\n", SDL_GetError() );
return false;
}
int flags=IMG_INIT_JPG|IMG_INIT_PNG;
int initted=IMG_Init(flags);
if((initted&flags) != flags){
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
return false;
//renderer init fail
}
else{
//IMG_INIT was a success
SDL_Surface *m_pSurface;
m_pSurface = IMG_Load("background.png");
if(m_pSurface == NULL){
printf("Error while trying creating surface! SDL_image Error: %s\n",IMG_GetError());
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,m_pSurface);
if(m_pTexture == NULL){
printf("Error while trying creating texture! SDL_image Error: %s\n",IMG_GetError());
}
else{
//success creating the texture
SDL_FreeSurface(m_pSurface);
SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w, &m_sourceRectangle.h);
m_destinationRectangle.x=m_sourceRectangle.x = 0;
m_destinationRectangle.y=m_sourceRectangle.y = 0;
m_destinationRectangle.w=m_sourceRectangle.w;
m_destinationRectangle.h=m_sourceRectangle.h;
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,&m_destinationRectangle);;
SDL_RenderPresent(m_pRenderer);
}
}
}
else{
std::cout<<"Window creation failed\n";
return false;
//window init fail
}
}
else{
std::cout<<"SDL init fail\n";
return false;
//SDL init fail
}
std::cout<<"Initialization was a success\n";
return true; //everything initialized successfully, start the main loop
}
void Game::render(){
SDL_RenderClear(m_pRenderer); //clear the renderer to draw color
SDL_RenderPresent(m_pRenderer); //draw to the screen
}
void Game::handleEvents(){
SDL_Event event;
if(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT: m_bRunning =false; break;
default: break;
}
}
}
void Game::clean(){
std::cout<<"Cleaning game\n";
SDL_DestroyTexture(m_pTexture);
IMG_Quit();
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
Tested With:
OS: Linux
Libs: SDL2, SDL2_image
IDE: Netbeans
I'm getting the same log every time I run my program.
There is a snippet from the main.cpp, where every variable is in local scope:
EDIT: you find at the bottom.
I made these functions for start the game, run the menu loop, and close the graphics, and give false value for gameNoError if something goes wrong. But the last function gives me the following log message:
"No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!"
(Note, that I wrote all log messages myself, because I want to use logging to.)
So, this means that I loose somewhere the value of pointers, but I can't find out why?
There is my close function too:
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
I only use the pointers for rendering in menu loop (if you need I will paste that too!) and the pointers are used in scope.
So what is the problem?
EDIT:
I found something: the menu loop looses the pointers too! But SDL starts and I made this not first. So I really don't know what is the problem.
Files from my project:
main.cpp:
#include <stdio.h>
#include <string>
#include <SDL.h>
#include "initialization/utility.hpp"
#include "initialization/init_closeGraphics.hpp"
#include "menuLoop.hpp"
int main(int argc, char* argv[])
{
bool gameNoError = true;
SDL_Renderer* gameRenderer = NULL;
SDL_Window* gameWindow = NULL;
FILE* logFile = fopen("errorLog.txt", "w");
doLogging(logFile, "Game started successfully!");
gameNoError = initGraphics(logFile, gameWindow, gameRenderer, "This is a title!", 800, 600);
if (gameNoError) gameNoError = doMenuLoop(logFile, gameWindow, gameRenderer, 800, 600);
if (gameNoError)
{
gameNoError = closeGraphics(logFile, gameWindow, gameRenderer);
}
else
{
closeGraphics(logFile, gameWindow, gameRenderer);
}
if (!gameNoError)
{
doLogging(logFile, "Game stopped with error/exception/problem!");
}
else
{
doLogging(logFile, "Game stopped successfully!");
}
return 0;
}
utility.hpp contains only the logging function
init_close.cpp: (it has it's header)
#include "init_closeGraphics.hpp"
bool initGraphics(FILE* logFile,
SDL_Window* gameWindow,
SDL_Renderer* gameRenderer,
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)
{
bool success = true;
if (gameWindowWidth <= 0 or gameWindowHeight <= 0)
{
doLogging(logFile, "Get less then or equal with 0 window dimensions at SDL initialization!");
success = false;
}
else
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
doLogging(logFile, "Failed to initialize SDL!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameWindow = SDL_CreateWindow(gameTitle,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
gameWindowWidth,
gameWindowHeight,
SDL_WINDOW_SHOWN);
if (gameWindow == NULL)
{
doLogging(logFile, "Failed to create window!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gameRenderer == NULL)
{
doLogging(logFile, "Failed to create renderer!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor(gameRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int IMG_FLAG = IMG_INIT_PNG;
if (!(IMG_Init(IMG_FLAG) & IMG_FLAG))
{
doLogging(logFile, "Failed to initialize SDL_image!");
doLogging(logFile, IMG_GetError());
success = false;
}
if (TTF_Init() == -1)
{
doLogging(logFile, "Failed to initialize SDL_ttf!");
doLogging(logFile, TTF_GetError());
success = false;
}
}
}
}
}
return success;
}
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
You are passing your pointer variables to initGraphics by value, so your call site will not see any changes to them.
If you want the variables in main to be updated when you call initGraphics, take them by reference:
bool initGraphics(FILE* logFile,
SDL_Window*& gameWindow,
// ^
SDL_Renderer*& gameRenderer,
// ^
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)
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.
When I run this code (from the Lazy Foo SDL tutorial) the program immediately shuts down. Why is that? I'm sorry if it gets kind of messy because of the lack of comments, but I thought that it didn't really matter since there were comments on Lazy Foo's post. I get no errors when building it.
#include "SDL/SDL_image.h"
#include "SDL/SDL.h"
#include <string>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *load_image (std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface (int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface (source, NULL, destination, &offset);
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Event test", NULL);
return true;
}
bool load_files()
{
image = load_image ("background.png");
if (image == NULL)
{
return false;
}
return true;
}
void clean_up()
{
SDL_FreeSurface(image);
SDL_Quit();
}
int main(int argc, char* args[])
{
bool quit = false;
if (init() == false)
{
return 1;
}
if (load_files() == false)
{
return 1;
}
apply_surface(0,0, image, screen);
if(SDL_Flip(screen) == -1)
{
return 1;
}
while(quit == false)
{
while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}
If you're missing background.png, or any of the required DLL's in the executable directory, you can encounter weird crashes.
Being that this is one of the simpler programs in this series, I'd bet this is the issue. I've seen strange segfaults myself when I had a brain-fart and forgot a file, or it wasn't where the exe expected it to be.