SDL_Surface not declared in this scope - c++

I seem to be having an issue with the use of SDL_Surface(s).
This is my header file (CApp.h):
#ifndef _CAPP_H_
#define _CAPP_H_
#include <SDL.h>
class CApp {
private:
bool Running;
SDL_Surface* Surf_Display;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif
and this is where the problem lies:
#include "CApp.h"
CApp::CApp() {
Surf_Display = NULL;
Running = true;
}
int CApp::OnExecute() {
if(OnInit() == false) {
return -1;
}
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(int argc, char* argv[]) {
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
CApp theApp;
return theApp.OnExecute();
}
The error say 'Surf_Display' was not declared in this scope.

Related

SDL2/SDL.H file not found

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 */

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?

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.

constructor is private error using singleton c++ and within this context error

i am following SDL game development book by shaun mitcchel and was trying to compile the code and was getting error of constructor is private. i am using mingw for compiling.
main.cpp
#include "game.h"
game* g_game = 0;
int main(int argc, char* args[])
{
g_game = new game;
g_game->init("SDL",100,100,640,480,SDL_WINDOW_SHOWN);
while(g_game->running())
{
g_game->handleEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
game.h
#ifndef GAME_H
#define GAME_H
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include "TextureManager.h"
class game
{
public:
game(){}
virtual ~game();
bool init(char* title, int xpos, int ypos, int width, int height, int flags);
void render();
void update();
void handleEvents();
void clean();
bool running()
{
return m_bRunning;
}
protected:
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
int m_currentFrame;
textureManager m_textureManager;
bool m_bRunning;
};
#endif // GAME_H
game.cpp
#include "game.h"
bool game::init(char* title,int xpos, int ypos, int width, int height,int flags)
{
//initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING)== 0)
{
std::cout << "SDL init success\n";
//init window
m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);
if(m_pWindow != 0)
{
std::cout << "window creation successful\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow,-1,0);
if(m_pRenderer != 0) {
std::cout << "render init success\n";
SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255);
}
else {
std::cout << "render init fail\n";
return false;
}
}
else {
std::cout << "Window init fail\n";
return false;
}
}
else {
std::cout << "Sdl init failed\n";
return false;
}
std::cout << "init success\n";
m_bRunning = true;
//to load
if(!ThetextureManager::Instance()->load("assets/animate- alpha.png","animate",m_pRenderer))
{
return false;
}
}
void game::render()
{
SDL_RenderClear(m_pRenderer);
ThetextureManager::Instance()->draw("animate", 0, 0,128,82,m_pRenderer);
ThetextureManager::Instance()->drawFrame("animate", 100,100,128,82,1,m_currentFrame,m_pRenderer);
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 game\n";
SDL_DestroyRenderer(m_pRenderer);
SDL_DestroyWindow(m_pWindow);
SDL_Quit();
}
void game::update()
{
m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
}
TextureManager.h
#ifndef TEXTUREMANAGER_H_INCLUDED
#define TEXTUREMANAGER_H_INCLUDED
#include <iostream>
#include<map>
#include<SDL.h>
#include<SDL_image.h>
class textureManager {
public:
static textureManager* Instance()
{
if(s_pInstance == 0)
{
s_pInstance = new textureManager();
return s_pInstance;
}
return s_pInstance;
}
bool load(std::string fileName, std::string id,SDL_Renderer* pRenderer);
void draw(std::string id,int x,int y,int width,int height,SDL_Renderer* pRenderer,SDL_RendererFlip flip);
void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer,SDL_RendererFlip flip);
std::map<std::string, SDL_Texture*> m_textureMap;
private:
textureManager() {}
textureManager(const textureManager&);
textureManager& operator=(const textureManager&);
static textureManager* s_pInstance;
};
typedef textureManager ThetextureManager;
#endif // TEXTUREMANAGER_H_INCLUDED
texturemanagement.cpp
#include "TextureManager.h"
textureManager* textureManager::s_pInstance = 0;
bool textureManager::load(std::string fileName, std::string id,SDL_Renderer* pRenderer)
{
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
if(pTempSurface == 0)
{
return false;
}
SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
if(pTexture != 0)
{
m_textureMap[id] = pTexture;
return true;
}
return false;
}
void textureManager::draw(std::string id,int x,int y,int width,int height,SDL_Renderer* pRenderer,SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect,0 , 0, flip);
}
void textureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer,SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = width * currentFrame;
srcRect.y = height * (currentRow - 1);
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect,0, 0, flip);
}
errors
C:\Users\lenovo\Desktop\node project\sdl\TextureManager.h||In constructor 'game::game()':|
C:\Users\lenovo\Desktop\node project\sdl\TextureManager.h|28|error: 'textureManager::textureManager()' is private|
C:\Users\lenovo\Desktop\node project\sdl\game.h|12|error: within this context|
C:\Users\lenovo\Desktop\node project\sdl\game.cpp||In member function 'void game::render()':|
C:\Users\lenovo\Desktop\node project\sdl\game.cpp|46|error: no matching function for call to 'textureManager::draw(const char [8], int, int, int, int, SDL_Renderer*&)'|
C:\Users\lenovo\Desktop\node project\sdl\game.cpp|47|error: no matching function for call to 'textureManager::drawFrame(const char [8], int, int, int, int, int, int&, SDL_Renderer*&)'|
You have textureManager m_textureManager member in class game (file game.h)
But in game.cpp you always use ThetextureManager::Instance()
You just need to remove m_textureManager member.

Undefined reference to class::function in c++

When I try to call OnLoop I get error that it doesn't recognise it.
///Ins_App.h
#ifndef INS_APP_H
#define INS_APP_H
#include <SDL/SDL.h>
class Ins_App
{
private:
/* Variables */
bool Running;
SDL_Surface* Surf_Display;
public:
/* inMain */
Ins_App();
int OnExecute();
public:
/* Other */
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
protected:
};
#endif // INS_APP_H
///Ins_App.cpp
#include "Ins_App.h"
Ins_App::Ins_App()
{
Running = true;
Surf_Display = NULL;
}
int Ins_App::OnExecute(){
if(OnInit() == false){
return -1;
}
SDL_Event Event;
while(Running){
while(SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
return 0;
}
int main(int argc, char* argv[]){
Ins_App iApp;
return iApp.OnExecute();
}
///OnLoop.cpp
#include "Ins_App.h"
void OnLoop(){
}
And here is the error:
obj\Debug\src\Ins_App.o:C:\Users\Al\Documents\Ins
\src\Ins_App.cpp|19|undefined reference to `Ins_App::OnLoop()'|
What am I doing wrong?
You didn't define your member:
void OnLoop(){
}
should be
void Ins_App::OnLoop(){
}
You're basically just defining a free function named OnLoop, not your member.