SDL2/SDL.H file not found - c++

I'm following this tutorial on youtube:
https://www.youtube.com/watch?v=44tO977slsU
and he links a video on how to install SDL2, I follow that video, and I followed this one as well. But when I compile it keeps coming up with SDL2/SDL not found? I'm not sure how to fix this
Here is my code so far
MAIN.CPP
#include "Game.hpp"
Game *game =nullptr;
int main(int argc, const char * argv[]) {
game = new Game();
game->init("Nijigen-Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running()) {
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
Game.cpp
#include "Game.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 Initiated!..." << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(window)
{
std::cout << "Window 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()
{}
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;
}
** And Game.hpp**
#ifndef Game_hpp
#define Game_hpp
#include "SDL2/SDL.h"
#include <iostream>
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:
bool isRunning;
SDL_Window *window;
SDL_Renderer *renderer;
};
#endif /* Game_hpp */

Related

LNK1120 error and LNK2019 error inside include line game dev?

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?

How to fix SDL2 'Assertion failure at SDL_RenderClear_REAL' in C++?

I'm learning the basics of creating a window using SDL2 by following a tutorial. The code works just fine, and even compiles correctly, however on runtime it gives the error message 'Assertion failure at SDL_RenderClear_REAL'.
I've tried reinstalling SDL2, as well as moving it to the user library folder on my mac, but neither of these fixed the issue
main.cpp
#include "game.hpp"
Game *game = nullptr;
int main() {
game = new Game();
game->init("GUI", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running()) {
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
game.cpp
#include "game.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 == true) {
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "Sub-systems initialized\n";
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window) {
std::cout << "Window created.\n";
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Renderer created\n";
}
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() {
cnt++;
std::cout << cnt << "\n";
}
void Game::render() {
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void Game::clean() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game cleaned\n";
}
game.hpp
#ifndef game_hpp
#define game_hpp
#include "SDL2/SDL.h"
#include <iostream>
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 cnt = 0;
bool isRunning;
SDL_Window *window;
SDL_Renderer *renderer;
};
#endif
After narrowing down the code, it seems that the issue is limited to SL_RenderClear. Here's the exact message it produces in the console:
2019-08-20 15:26:15.508900-0500 GUI[26436:7287503] WARN:
Assertion failure at SDL_RenderClear_REAL (/Users/valve/release/SDL/SDL2-2.0.10-source/src/render/SDL_render.c:2235), triggered 1 time:
'renderer && renderer->magic == &renderer_magic'
Program ended with exit code: 42
How can I fix this issue?
EDIT: Added full code to help identify the problem.
A basic "hello world" of creating a window and clearing the color on the screen for SDL requires a few boilerplate initialization steps
Initialize SDL
Create a SDL_Window
Create a SDL_Renderer
After which you can do
SDL_RenderClear and use SDL_RenderDrawColor to set a custom color to make it more obvious that it is properly clearing the Renderer.
Without error checking this would look something like:
SDL_Init(flags);
SDL_Window* window = SDL_CreateWindow("window title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, more_flags);
SDL_SetRenderDrawColor(renderer, red, green, blue, alpha);
SDL_RenderClear(renderer);
Also remember to call SDL_Delay(ms) if you want the program to not immediately close before you get a chance to see the window.

Window not appearing in SDL2 (C++)

I have recently started a game project in C++ using SDL2. I have created a class for the Game and included an init function which initializes SDL and creates a window. My code complies normally with 0 errors and warnings. However, when I run the executable file, I am not able to terminate the program nor a window appears.
My Code:
game.cc
#include "Game.hh"
#include <iostream>
using namespace std;
// Constructing Funtions
Game::Game()
{}
Game::~Game()
{}
// Initializing Function
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
// Check to see if Fullscreen Mode has been called
if(fullscreen)
flags = SDL_WINDOW_FULLSCREEN;
if(SDL_Init(SDL_INIT_EVERYTHING) == 1) {
cout << "Initialization Complete" << endl;
// Create the Window
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
// Create the Renderer
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
// Set Is Running to True
isRunning = true;
} else {
isRunning = false;
}
}
// Looping Funtions
void Game::eventHandler()
{
// Initialize EventHandler & Get Event Type
SDL_Event event;
SDL_PollEvent(&event);
// Process Events
switch (event.type) {
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
void Game::update()
{}
void Game::render()
{
// Clear Renderer
SDL_RenderClear(renderer);
// Render Shit
SDL_RenderPresent(renderer);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
cout << "Game Quit Successfuly" << endl;
}
bool Game::running()
{
return isRunning;
}
Game.hh
#ifndef GAME_HH
#define GAME_HH
#include "SDL.h"
class Game {
public:
// Constructor and Deconstructor
Game();
~Game();
// Initializing Functions
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
// Looping Commands
void eventHandler();
void update();
void render();
void clean();
// Application Running Checker
bool running();
private:
bool isRunning;
SDL_Window* window;
SDL_Renderer* renderer;
};
#endif
And finally main.cc
#include <iostream>
#include "Game.hh"
Game *game = nullptr;
int main()
{
game = new Game;
game->init("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
do {
game->eventHandler();
game->update();
game->render();
} while(game->running());
game->clean();
}
Any Ideas?
EDIT: found a fix to the issue, I made the public function running return the private isRunning variable. However, now the issue is that the code returns "Game Quit Successfully" w/out initiating anything...

Breaking up the SDL code

I am doing first examples from the "SDL Game Development Black and White" book.
The point where I have problems is in example where I should break "HelloSDL" code in one header( .h) and two cpp( .cpp) files.
Here they are:
main.cpp
#include "Game.h"
#include <stdio.h>
#include <SDL2/SDL.h>
Game* g_game = 0;
int main(int argc, char* args[])
{
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();
void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default;
break;
}
}
}
return 0;
}
Game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <SDL2/SDL.h>
class Game
{
public:
Game();
~Game();
void init();
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 // GAME_H_INCLUDED
Game.cpp
#include <iostream>
#include "Game.h"
//#include <stdio.h>
#include <SDL2/SDL.h>
bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init success\n";
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, 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, 255 255, 255, 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_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
SDL_Quit()
+{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
After compiling it in CodeBlocks(16.11) I get next error messages:
Game.cpp|6|error: prototype for 'bool Game::init(const char*, int, int, int, int, int)' does not match any in class 'Game'
Game.h|12|error: candidate is: void Game::init()
Firstly, I got much more errors but I managed to solve it.
There is still two more errors to solve.
I suppose that is something wromg in Game.h file, but I cannot find it.
Thank you in advance!
The init() declared in the game.h returns void, but the definition you provide returns bool.
It seems that you may be coming from C background. In C++ omitting function signature at function declaration is not permitted. Basically by declaring void init(); you declare void Game::init(void) function, not bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags). So one must carefully check that function declaration does exactly match function definition. Another thing is that in C++ one should perform object initialization at constructor and cleanup at destructor.

SDL2 errors what am I doing Wrong

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.