I Google-d this problem and viewed at least 20 threads about this and tried different solutons but none worked out.This code is in SDL 2.0.3 in Visual Studio 2013 Express. Here is my code:
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
class MGGMain{
int player_x;
int player_y;
int player_w = 50;
int player_h = 50;
SDL_Rect player_rect;
SDL_Texture* player_text;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event* mainEvent;
bool quit;
void Init()
{
}
void LoadAssets(SDL_Renderer* renderer)
{
//IMG_LoadTexture(renderer, <path>);
player_text = IMG_LoadTexture(renderer, "images/dev/dev_player");
}
void Update()
{
int player_x;
int player_y;
SDL_Rect player_rect = CreateRect(player_h, player_w, player_x, player_y);
if (mainEvent->type == SDL_QUIT)
{
quit = true;
}
}
void Draw(SDL_Renderer* renderer)
{
//SDL_RenderCopy(renderer, <texture>, <rect>)
SDL_RenderCopy(renderer, player_text, &player_rect, &player_rect);
}
SDL_Rect CreateRect(int h, int w, int x, int y)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.h = h;
rect.w = w;
return rect;
}
int main(int argc, char* argv[])
{
std::cout << "Initialyzing SDL..." << std::endl;
SDL_Init(SDL_INIT_EVERYTHING);
std::cout << "All initialyzed, creating window and event system..." << std::endl;
window = SDL_CreateWindow("MGG", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1200, 700, SDL_WINDOW_SHOWN);
std::cout << "All succeded!" << std::endl;
quit = false;
if (window = NULL){
std::cout << "SDL Error: Window couldn't be created!" << std::endl;
return 0;
}
std::cout << "Creating renderer..." << std::endl;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
std::cout << "Renderer creation success, beginning main loop." << std::endl;
mainEvent = new SDL_Event();
LoadAssets(renderer);
Init();
while (!quit)
{
SDL_PollEvent(mainEvent);
Update();
SDL_RenderClear(renderer);
Draw(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
return 0;
}
};
Your program has no main() function.
The main function in your code is inside class MGGMain, so it becomes MGGMain::main(). It needs to be simply main(), in the highest scope, that is, outside any classes or the compiler will not find it.
SDL errors out because it overrides the main used by SDL programs to do some initializations and then calls the real main(), but since there is no main(), it throws unresolved external symbol error.
Related
I'm currently using this tutorial: https://www.youtube.com/watch?v=44tO977slsU&list=PLhfAbcv9cehhkG7ZQK0nfIGJC_C-wSLrx&index=3
But I've been getting this error on the first line that I don't know how to address as a beginner.
It says it's unresolved externals
I've been trying to look at some of the windows config advice, but I don't know how to apply it to this tutorial.
here's the code:
#include "Header.hpp"
Game::Game(){}
Game::~Game(){}
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen) { flags = SDL_WINDOW_FULLSCREEN; }
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "Subsystems initialized" << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window) { std::cout << "Windows created" << std::endl; }
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "renderer created" << std::endl;
}
isRunning == true; } else { isRunning == false; }
}
void Game::handleEvents() {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
void Game::update() { count++; std::cout << count << std::endl; }
void Game::render() {
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void Game::clean() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game cleaned" << std::endl;
}
this is is the header file, if it'll help:
#ifndef Header_hpp
#define Header_hpp
#include <iostream>
#include "SDL.h"
class Game {
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool running(){ return isRunning; }
private:
int count = 0;
bool isRunning;
SDL_Window *window;
SDL_Renderer *renderer;
};
#endif / * Header_hpp */
I'm genuinely clueless on what could be causing this so I guess I'll include the main.cpp as well if something can be found there:
#include "Header.hpp"
Game* game = nullptr;
int main(int argc, const char * argv[]) {
game = new Game();
game->init("Birchengine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running()) {
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
What could be the cause of this error, and how do I prevent it in the future?
I'm following along with a video tut on SDL. When I do exactly as he has done, by putting an SDL_Texture* in the Game.h and use the object in Game.cpp, I get an error about multiple definitions. Yet, the video creator has no error of such. I can, however, copy and paste the SDL_Texture* to the .cpp and it work just fine.
Why is it that the same code will work on one, but not another machine, in Visual Studio
Game.cpp
#include "Game.h"
Game::Game()
{
}
Game::~Game()
{
}
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "Successfully Initialized Subsystem . . . " << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window)
{
std::cout << "Successfully Created Window . . . " << std::endl;
}
else
{
std::cout << "Something went wrong. Error: " << SDL_GetError() << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Successfully Created Renderer . . . " << std::endl;
}
else
{
std::cout << "Something went wrong. Error: " << SDL_GetError() << std::endl;
}
is_running = true;
}
else
{
is_running = false;
}
SDL_Surface* surf_temp = IMG_Load("assets/player.png");
tex_player = SDL_CreateTextureFromSurface(renderer, surf_temp);
SDL_FreeSurface(surf_temp);
}
void Game::event_handle()
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
is_running = false;
}
}
void Game::update()
{
cnt++;
std::cout << cnt << std::endl;
}
void Game::render()
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, tex_player, NULL, NULL);
SDL_RenderPresent(renderer);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game cleaned . . . " << std::endl;
}
Game.h
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <stdio.h>
#include <string>
SDL_Texture* tex_player;
class Game
{
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void event_handle();
void update();
void render();
void clean();
bool running() { return is_running; }
private:
int cnt;
bool is_running;
SDL_Window *window;
SDL_Renderer *renderer;
};
main.cpp
#include "Game.h"
Game *game = nullptr;
int main(int argc, char *argv[])
{
game = new Game();
game->init("Yeah", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running())
{
game->event_handle();
game->update();
game->render();
}
game->clean();
return 0;
}
Error:
1>------ Build started: Project: SDL_Template, Configuration: Debug Win32 ------
1>main.cpp
1>LINK : C:\Users\Onion\documents\visual studio 2017\Projects\SDL_Template\Debug\SDL_Template.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2005: "struct SDL_Texture * tex_player" (?tex_player##3PAUSDL_Texture##A) already defined in Game.obj
1>C:\Users\Onion\documents\visual studio 2017\Projects\SDL_Template\Debug\SDL_Template.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Done building project "SDL_Template.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Think of #include as "please copy-paste the content of this file here."
Since you #includeGame.h in both main.cpp and Game.cpp, the line SDL_Texture* tex_player; which is a global variable definition (as opposed to declaration) appears in both files, which confuses the linker.
The immediate fix is to declare the variable in game.h:
extern SDL_Texture* tex_player;
and define it in game.cpp
SDL_Texture* tex_player;
The better fix would be to make tex_player a member variable of Game (or some other class).
I'm very new to C++ and SDL and I am trying to create a thread that constantly updates the screen but the I keep getting the following errors:
'std::invoke no matching overloaded function found'
and
'Failed to specialize function template 'unknown-type std::invoke(Callable &&,_Types&&...)''
main.cpp
int main(int argc, char **argv) {
using namespace std::placeholders;
bool gameover = false;
int test;
std::string filepath = getResourcePath("Lesson1");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { // Intializes SDL functionality
std::cout << "Could not start SDL" << std::endl;
std::cin >> test;
return 1;
}
else {
std::cout << "SDL started successfully!" << std::endl;
}
viewWindow window; // Class representing the window in which the program is run.
SDL_Renderer *render = window.render(); // Pointer to the renderer used to draw images to the window.
if (render == nullptr) {
std::cout << "There was an error creating the renderer" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
SDL_Surface *emptySurface = window.blankSurface(); // Temp surface to draw background to
if (emptySurface == nullptr) {
std::cout << "Unable to create a blank surface " << std::endl << SDL_GetError() << std::endl;;
std::cin >> test;
return 1;
}
surfaces background;
background.filename = "grass.bmp";
SDL_Surface *backgroundSurface = background.loadSurface(filepath);
if (backgroundSurface == nullptr) {
std::cout << "Unable to create background surface" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
SDL_Rect backgroundRect;
SDL_Texture *backTexture = background.blitBack(render, backgroundRect, backgroundSurface, emptySurface);
player player;
SDL_Rect playerRect;
playerRect.x = 320;
playerRect.y = 240;
playerRect.h = 16;
playerRect.w = 16;
SDL_Texture *playerTexture = player.createPlayerTexture(render, filepath);
if (playerTexture == nullptr) {
std::cout << "Could not load player texture" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
while (!gameover) {
std::thread t((&viewWindow::refreshWindow, render, playerRect, backTexture, playerTexture));
playerRect.x = player.moveX(playerRect);
playerRect.y = player.moveY(playerRect);
t.join();
}
return 0;
}
viewWindow.h
#pragma once
#ifndef VIEWINDOW_H
#define VIEWWINDOW_H
#include "SDL.h"
class viewWindow // Class representing the window.
{
private:
char winName[45] = "Game Test";
int winWidth = 640;
int winHeight = 480;
int xPos = 960;
int yPos = 540;
public:
SDL_Window *view(); // Intializing funtions for creating the window and renderer.
SDL_Renderer *render();
SDL_Surface *blankSurface();
void refreshWindow(SDL_Renderer *renderer, SDL_Rect &playerRect, SDL_Texture *backtex, SDL_Texture *playertex);
};
#endif
viewWindow.cpp
#include "viewWindow.h"
#include <string>
#include "SDL.h"
SDL_Window *viewWindow::view()
{
SDL_Window *createdwindow = SDL_CreateWindow(winName, xPos, yPos, winWidth, winHeight, SDL_WINDOW_SHOWN);
return createdwindow;
}
SDL_Renderer *viewWindow::render() {
SDL_Renderer *render = SDL_CreateRenderer(view(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
return render;
}
SDL_Surface *viewWindow::blankSurface() {
SDL_Surface *blacksurface = SDL_CreateRGBSurface(0, winWidth, winHeight, 32, 0, 0, 0, 0);
return blacksurface;
}
void viewWindow::refreshWindow(SDL_Renderer *renderer, SDL_Rect &playerRect, SDL_Texture *backtex, SDL_Texture *playertex) {
while (true) {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backtex, NULL, NULL);
SDL_RenderCopy(renderer, playertex, NULL, &playerRect);
SDL_RenderPresent(renderer);
}
}
Method refreshWindow is not static. std::invoke requires the object instance of viewWindow class to call this method. You should pass it as a second parameter into the thread constructor:
std::thread t(&viewWindow::refreshWindow, window, render, std::ref(playerRect), backTexture, playerTexture);
Instead of function pointer you could use lambda function:
std::thread t([&](viewWindow* view){ view->refreshWindow(render, playerRect, backTexture, playerTexture); }, &window);
It was working before I tried to load my image.
This is the error I get:
Error 1 error LNK2005: "struct SDL_Window * m_pWindow"
(?m_pWindow##3PAUSDL_Window##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 2 error LNK2005: "struct SDL_Renderer * m_pRenderer"
(?m_pRenderer##3PAUSDL_Renderer##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 3 error LNK2005: "struct SDL_Texture * m_pTexture"
(?m_pTexture##3PAUSDL_Texture##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 4 error LNK2005: "struct SDL_Rect m_sourceWREK"
(?m_sourceWREK##3USDL_Rect##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 5 error LNK2005: "struct SDL_Rect m_destWREK"
(?m_destWREK##3USDL_Rect##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 7 error LNK1169: one or more multiply defined symbols found
C:\Users\Joseph\Desktop\DuckGotti\Debug\DuckGotti.exe 1
Here is my code (main.cpp):
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Game.h"
/**my first game obj**/
Game* g_game = 0;
int main(int argc, char* argv[])
{
g_game = new Game();
g_game->init("Duck Gotti", 100, 100, 640, 480, SDL_WINDOW_FULLSCREEN );
while (g_game->running())
{
g_game->handelEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
Game.h
#ifndef __Game__
#define __Game__
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;
class Game
{
public:
Game(){}
~Game(){}
/**setting the run var to true**/
bool init(const char* title, int xpos, int ypos, int width, int height
,bool fullscreen);
void render();
void update();
void handelEvents();
void clean();
/** a func to access the privet var **/
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"
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 initalize SDL**/
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init sucess/n";
/**init the winn**/
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0)
{
std::cout << "window creation sucess/n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if (m_pRenderer != 0)
{
std::cout << "renderer created/n";
SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
}
else
{
std::cout << "renderer broked/n";
return false;
}
}
else
{
std::cout << "winn init nope/n";
return false;
}
}
else
{
std::cout << "SDL init broked/n";
return false;
}
std::cout << "init sucess/n";
m_bRunning = true;
return true;
SDL_Surface* pTempSurf = SDL_LoadBMP("img3.bmp");
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurf);
SDL_FreeSurface(pTempSurf);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceWREK.w, &m_destWREK.h);
m_destWREK.x = m_sourceWREK.x = 0;
m_destWREK.y = m_sourceWREK.y = 0;
m_destWREK.w = m_sourceWREK.w;
m_destWREK.h = m_sourceWREK.h;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceWREK, &m_destWREK);
SDL_RenderPresent(m_pRenderer);
}
void Game::update()
{
}
void Game::handelEvents()
{
SDL_Event evil;
if (SDL_PollEvent(&evil))
{
switch (evil.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
I believe you are reading this book SDL Game Development. Be aware, the book has a bunch of errors.
remove these lines
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;
They are already defined inside game class.
I was reading this book, luckily I was organized and I kept the files. It seems you are in the first chapter
main.cpp
#include "game.h"
Game* g_game = 0;
int main(int argc, char* args[])
{
g_game = new Game();
g_game->init("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
while( g_game->running() ){
g_game->handleEvents();
g_game->update();
g_game->render();
SDL_Delay(10);
}
g_game->clean();
return 0;
}
game.h
#ifndef _GAME_H
#define _GAME_H
#include "SDL.h"
class Game
{
public:
Game() {}
~Game() {}
bool init(const char* title, int xpos, int ypos, int height, int width, int flags);
void render();
void update();
void handleEvents();
void clean();
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(_GAME_H) */
game.cpp
#include "game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags)
{
if ( SDL_Init(SDL_INIT_EVERYTHING) == 0 ){
std::cout << "SDL init success\n";
m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
if ( m_pWindow != 0 ){
std::cout << "window creation success \n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if ( m_pRenderer != 0 ){
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}else{
std::cout << "renderer init fail\n";
return false;
}
}else{
std::cout << "window init fail\n";
return false;
}
}else{
std::cout << "SDL init fail\n";
return false;
}
std::cout << "init success\n";
m_bRunning = true;
//#####################################################################################
return true;
}
void Game::render()
{
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::handleEvents()
{
SDL_Event event;
if ( SDL_PollEvent(&event) ){
switch( event.type ){
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::update()
{
}
These are some notes I've written
After chapter 2, you need to organize your classes because you will get lost easily with the inheritance. For example, the below picture, you will see the way classes are connected in chapter 3
For chapter 4,
Hope this helps.
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);