I'm trying to render a BMP image, but nothing is showing up other than the draw color. I have most of my code in a header file.
[game.h]
#ifndef __Game__
#define __Game__
#include <string>
#include "SDL.h"
using namespace std;
class Game {
public:
Game();
bool init(const char*, int, int, int, int, int);
bool isRunning();
void update();
void render();
void eventHandle();
void exit();
SDL_Texture* getTexture();
SDL_Texture* loadBMPTexture(const char*);
private:
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture;
bool running;
};
Game::Game() {}
bool Game::init(const char* winTitle, int xpos, int ypos, int width, int height, int flags) {
bool success = true;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "An Error Has Occured", ("SDL_Error:" + ((string)SDL_GetError())).c_str() , NULL);
bool success = false;
}
else {
window = SDL_CreateWindow(winTitle, xpos, ypos, width, height, flags);
if (window == NULL) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "An Error Has Occured", ("SDL_Error:" + ((string)SDL_GetError())).c_str(), NULL);
bool success = false;
}
else {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "An Error Has Occured", ("SDL_Error:" + ((string)SDL_GetError())).c_str(), NULL);
bool success = false;
}
else
{
SDL_Rect srcRect;
SDL_Rect destRect;
SDL_Surface* pTempSurface = SDL_LoadBMP("./resources/img4.bmp");
if (pTempSurface == NULL) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "An Error Has Occured", ("SDL_Error:" + ((string)SDL_GetError())).c_str(), NULL);
}
SDL_FillRect(pTempSurface,NULL,0xFFFFFFFF);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
destRect.x = srcRect.x = 0;
destRect.y = srcRect.y = 0;
SDL_QueryTexture(texture,NULL,NULL,&srcRect.w, &srcRect.h);
cout << srcRect.w << endl;
int w, h;
SDL_GetRendererOutputSize(renderer,&w,&h);
destRect.w = w;
destRect.h = h;
cout << w;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0);
SDL_RenderCopy(renderer, texture, &destRect, &srcRect);
SDL_RenderPresent(renderer);
running = true;
}
}
}
return success;
}
void Game::render() {
//SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void Game::eventHandle() {
SDL_Event e;
if (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
running = false;
break;
}
}
}
void Game::exit() {
SDL_DestroyWindow(window);
window = NULL;
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_Quit();
}
bool Game::isRunning() {
return running;
}
SDL_Texture* Game::loadBMPTexture(const char* path) {
SDL_Surface* tempSurface = SDL_LoadBMP(path);
SDL_Texture* returnTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
SDL_FreeSurface(tempSurface);
return returnTexture;
}
void Game::update() {
}
SDL_Texture* Game::getTexture() {
return texture;
}
#endif
[main.cpp]
#include "SDL.h"
#include <string>
#include <iostream>
#include "Game.h"
using namespace std;
int SCREEN_WIDTH = 1280;
int SCREEN_HEIGHT = 720;
const Uint8* keystate;
//keystate = SDL_GetKeyboardState(NULL);
Game* game = NULL;
int main(int argc, char* argv[]) {
game = new Game();
game->init("Game Title [Confused]",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
while (game->isRunning()) {
game->eventHandle();
game->render();
}
game->exit();
return 0;
}