Texture creating is returning NULL - c++

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

Related

SDL_BlitSurface() blackscreen

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.

c++ with SDL not running

i've installed SDL to develop a program, and while on the test phase for this lib, the compiled code does not execute, and i mean it won't even open the cmd box. The weird thing is, it occasionally executes.
I'm using Eclipse Hellios with minGW32 and i686-w64-mingw32 on windows10.
My test code is:
#include <iostream>
#include <Windows.h>
#include <SDL2/SDL.h>
int main(int, char**)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return 1;
SDL_Window *window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);
if (window == NULL)
{
std::cout << "SDL init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *background = SDL_LoadBMP("Resources/Lilothyn.bmp");
if (background == NULL)
{
SDL_ShowSimpleMessageBox(0, "Background init error", SDL_GetError(), window);
return 1;
}
if (renderer == NULL)
{
SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, background);
if (texture == NULL)
{
SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
}
SDL_RenderPresent(renderer);
SDL_Event event;
bool running = true;
while(running)
{
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
running = false;
SDL_DestroyTexture(texture);
SDL_FreeSurface(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
break;
default: break;
}
}
return 0;
}
the odd thing is, i previously had "SDL_LoadBMP("Resources/TommyBMP.bmp");" and it executed properly. Changing a string shouldn't make the program stop working, both files are at the Debug/Resources folder so it can't be a case of not finding them...
note: further testing shows that a simple "hello world" has the same issue, so the problem will likely not be from SDL.
"help me obi wan, you're my only hope".

SDL image only shows occasionally

The code below is meant to show a picture of a rider in the top left corner of the SDL window. With this code the image occasionally does appear as intended, but most of the time when I run the program I just get a black window. Any idea why?
#include "Game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullScreen)
{
int flags = 0;
if (fullScreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
//attempt to initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING)==0) {
std::cout << "SDL init success \n";
//init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0) // window init successful
{
std::cout << "Window creation successful\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer !=0) // renderer init success
{
std::cout << "renderer creation successful\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}
else
{
std::cout << "renderer init failed!\n";
return false; // render init failed
}
}
else
{
std::cout << "Window init failed!\n";
return false; //window init failed
}
}
else
{
std::cout << "SDL init failed!\n";
return false; // SDL init failed
}
std::cout << "Init success\n";
m_bRunning = true; // everything init-d successfully, main game loop running
SDL_Surface* pTempSurface = SDL_LoadBMP("rider.bmp");
if(!pTempSurface)
{
std::cout << "Error, tempsurface is null";
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceRectangle.w, &m_sourceRectangle.h);
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.h = m_sourceRectangle.h;
m_destinationRectangle.w = m_sourceRectangle.w;
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle, &m_destinationRectangle);
SDL_RenderPresent(m_pRenderer);
}
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 app\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

SDL not responding after running for a few seconds on linux

I was following the SDL Game development book and I cant even get the first file to work right. Upon the application starting it renders the window and then after a few seconds Linux says the game is not responding and ask me to force quit. This repeats every few seconds if I click wait. One thing I have noticed is that SDL_PollEvent never returns true.I am not sure why things are not working. Here is my code.
Main.cpp
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include "Game.h"
// our Game object
Game* g_game = 0;
int main(int argc, char* argv[]) {
g_game = new Game();
g_game->init("Chapter 1", 100, 100, 640, 480, 0);
while(g_game->running()) {
g_game->handleEvents();
//g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
Game.h
#ifndef __Game__
#define __Game__
#if !WINDOWS
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif
class Game {
public:
Game() {}
~Game() {}
// simply set the running variable to true
bool init(const char* title, int xpos, int ypos, int width, int
height, bool fullscreen);
void render();
void update();
void handleEvents();
void clean();
// a function to access the private running variable
bool running() {
return m_bRunning;
}
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(__Game__) */
Game.cpp
#include "Game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen) {
// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "SDL init success\n";
// init the window
int flags = 0;
if(fullscreen) {
flags = SDL_WINDOW_FULLSCREEN;
}
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, 0);
if(m_pRenderer != 0) { // renderer init success
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer,
255,0,255,255);
} else {
std::cout << "renderer init fail\n";
return false; // renderer init fail
}
} else {
std::cout << "window init fail\n";
return false; // window init fail
}
} else {
std::cout << "SDL init fail\n";
return false; // SDL init fail
}
std::cout << "init success\n";
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}
void Game::render() {
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderPresent(m_pRenderer); // draw to the screen
}
void Game::clean() {
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::handleEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
std::cout << "Checking Events";
switch(event.type) {
case SDL_QUIT:
std::cout << "Quiting";
m_bRunning = false;
break;
default:
break;
}
}
}
Edit:
so I made a minamal version of the code and now i get a new error. The error says "Segmentation Fault (core dumped)". Based on running it to a specific line in debug the error seems to appear at the line that says "SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);" I am not sure what the error is though
Here is the minial code:
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if(win == NULL) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(ren == NULL) {
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
std::string imagePath = "cb.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
if(bmp == NULL) {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
if(tex == NULL) {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_FreeSurface(bmp);
SDL_Event e;
bool quit = false;
while(!quit) {
while(SDL_PollEvent(&e)) {
//If user closes the window
if(e.type == SDL_QUIT) {
quit = true;
}
//If user presses any key
if(e.type == SDL_KEYDOWN) {
quit = true;
}
//If user clicks the mouse
if(e.type == SDL_MOUSEBUTTONDOWN) {
quit = true;
}
}
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
}
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
I have compiled and run your minimal code successfully under Linux with SDL 2.0.4. I used a simple image as "cb.bmp".
Try the following :
compile like this : g++ -Wall -ggdb $(sdl2-config --cflags) -o program main.cpp -lSDL2
gdb ./program, then type 'run'
when it crashes, type 'bt' (for backtrace)
copy the output here
My guess is : either the "bmp" file format is not supported or the rendering driver is not supported. try to use :
SDL_CreateRenderer(win, -1, 0);

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.