(C++) Function not declaring - c++

I'm new to SDL, and I have a function bool init(). I've been learning SDL 2.0 from lazyfoot.net for a few days without a problem. I've been essentially copying their code, and today I tried to do my own. (EDIT: I know someone said I have an outdated header file. This is not the case, because I have been able to use SDL_WINDOWPOS_UNDEFINED and other 2.0 features ever since I installed the library) For whatever reason, this won't work.
I'm using Windows 7 Starter, with Code::Blocks (MinGW as my compiler)
When I (try to) build and run, there is a red flag next to line 17, which is this.
bool init() {
Here is the entire source code.
#include <iostream>
#include <SDL.h>
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
bool init();
bool loadMedia();
void quit();
int time = 5000;
SDL_Window* window = NULL;
SDL_Surface* surface = NULL;
SDL_Surface* topImage = NULL;
bool init() {
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << SDL_GetError() << std::endl;
success = false;
}
else{
SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL) {
std::cout << SDL_GetError() << std::endl;
success = false;
}
}
return success;
}
bool loadMedia() {
bool success = true;
surface = SDL_GetWindowSurface(window);
if(surface == NULL) {
std::cout << SDL_GetError() << std::endl;
success = false;
}
else {
topImage = SDL_LoadBMP("image.bmp");
if(topImage = NULL) {
std::cout << SDL_GetError() << std::endl;
success = false;
}
else {
SDL_BlitSurface(topImage, NULL, surface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(time);
}
}
return success;
}
void quit() {
SDL_FreeSurface(topImage);
topImage = NULL;
SDL_DestroyWindow(window);
window = NULL;
surface = NULL;
SDL_Quit();
}
int main(int argc, char* args[]) {
if(init() == true) {
init();
if(loadMedia() == true) {
loadMedia();
}
}
quit();
return 0;
}

You are getting this error:
error: 'SDL_WINDOWPOS_UNDEFINED' was not declared in this scope
which means that your including headers for an older version of SDL. but SDL_WINDOWPOS_UNDEFINED is defined in the SDL 2.0 version.
Just make sure to include the right headers which you can download them from the official library website: http://www.libsdl.org/download-2.0.php

Related

SDL 2 blitting BMP File

Currently trying to display a BMP file on a window through SDL 2
This is my main.cpp file:
`#include "pch.h"
#include <iostream>
#include<SDL.h>
using namespace std;
int main(int argc, char* args[])
{
bool run=true;
sdl a;
if (a.init() == false)
{
cout << "SDL not working" << endl;
return 0;
}
else
if (a.screendisplay() == false)
{
cout << "Enable to open the window" << endl;
return 0;
}
else
if (a.loadmedia() == false)
{
cout << "Unable to load the image" << endl << SDL_GetError()<<endl;
return 0;
}
else
a.imageprocessing();
SDL_Delay(2000);
a.quit();
return 0;
}`.
This is my pch.H file,which was mentioned above in the code:
class sdl
{
public:
bool init();
bool screendisplay();
bool quit();
bool loadmedia();
bool imageprocessing();
private:
SDL_Surface *gsurface = NULL;
SDL_Window * Window = NULL;
SDL_Surface * screenSurface = NULL;
};
Pch.cpp file:
#include "pch.h"
using namespace std;
bool sdl::init()
{
bool sucess = true;
if (SDL_Init(SDL_INIT_VIDEO) <0)
{
cout << "SDL not Working properply" << endl;
return false;
}
else return true;
};
bool sdl::screendisplay()
{
bool sucess;
Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
if (Window = NULL)
{
return sucess = false;
}
else
screenSurface= SDL_GetWindowSurface(Window);
return sucess = true;
};
bool sdl::loadmedia()
{
bool success = true;
gsurface = SDL_LoadBMP("asd.bmp");
if (gsurface == NULL)
{
success = false;
}
return success;
};
bool sdl::imageprocessing()
{
bool success = true;
if (SDL_BlitSurface(gsurface, NULL, screenSurface, NULL) < 0)
{
return success = false;
}
else
SDL_UpdateWindowSurface(Window);
SDL_Delay(2000);
return success;
};
bool sdl::quit()
{
bool sucess;
SDL_DestroyWindow(Window);
SDL_Quit();
return sucess = true;
};
When i run this program,the window appears but with no image. What am i doing wrong here?
I think the problem is in your screendisplay() function:
bool sdl::screendisplay()
{
bool sucess;
Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
if (Window = NULL) // <- Right here
{
return sucess = false;
}
else
screenSurface= SDL_GetWindowSurface(Window);
return sucess = true;
};
You're doing an assignment rather than a comparison. You should turn up your compiler warnings so it catches this.

Losing pointer values

I'm getting the same log every time I run my program.
There is a snippet from the main.cpp, where every variable is in local scope:
EDIT: you find at the bottom.
I made these functions for start the game, run the menu loop, and close the graphics, and give false value for gameNoError if something goes wrong. But the last function gives me the following log message:
"No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!"
(Note, that I wrote all log messages myself, because I want to use logging to.)
So, this means that I loose somewhere the value of pointers, but I can't find out why?
There is my close function too:
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
I only use the pointers for rendering in menu loop (if you need I will paste that too!) and the pointers are used in scope.
So what is the problem?
EDIT:
I found something: the menu loop looses the pointers too! But SDL starts and I made this not first. So I really don't know what is the problem.
Files from my project:
main.cpp:
#include <stdio.h>
#include <string>
#include <SDL.h>
#include "initialization/utility.hpp"
#include "initialization/init_closeGraphics.hpp"
#include "menuLoop.hpp"
int main(int argc, char* argv[])
{
bool gameNoError = true;
SDL_Renderer* gameRenderer = NULL;
SDL_Window* gameWindow = NULL;
FILE* logFile = fopen("errorLog.txt", "w");
doLogging(logFile, "Game started successfully!");
gameNoError = initGraphics(logFile, gameWindow, gameRenderer, "This is a title!", 800, 600);
if (gameNoError) gameNoError = doMenuLoop(logFile, gameWindow, gameRenderer, 800, 600);
if (gameNoError)
{
gameNoError = closeGraphics(logFile, gameWindow, gameRenderer);
}
else
{
closeGraphics(logFile, gameWindow, gameRenderer);
}
if (!gameNoError)
{
doLogging(logFile, "Game stopped with error/exception/problem!");
}
else
{
doLogging(logFile, "Game stopped successfully!");
}
return 0;
}
utility.hpp contains only the logging function
init_close.cpp: (it has it's header)
#include "init_closeGraphics.hpp"
bool initGraphics(FILE* logFile,
SDL_Window* gameWindow,
SDL_Renderer* gameRenderer,
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)
{
bool success = true;
if (gameWindowWidth <= 0 or gameWindowHeight <= 0)
{
doLogging(logFile, "Get less then or equal with 0 window dimensions at SDL initialization!");
success = false;
}
else
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
doLogging(logFile, "Failed to initialize SDL!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameWindow = SDL_CreateWindow(gameTitle,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
gameWindowWidth,
gameWindowHeight,
SDL_WINDOW_SHOWN);
if (gameWindow == NULL)
{
doLogging(logFile, "Failed to create window!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gameRenderer == NULL)
{
doLogging(logFile, "Failed to create renderer!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor(gameRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int IMG_FLAG = IMG_INIT_PNG;
if (!(IMG_Init(IMG_FLAG) & IMG_FLAG))
{
doLogging(logFile, "Failed to initialize SDL_image!");
doLogging(logFile, IMG_GetError());
success = false;
}
if (TTF_Init() == -1)
{
doLogging(logFile, "Failed to initialize SDL_ttf!");
doLogging(logFile, TTF_GetError());
success = false;
}
}
}
}
}
return success;
}
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
You are passing your pointer variables to initGraphics by value, so your call site will not see any changes to them.
If you want the variables in main to be updated when you call initGraphics, take them by reference:
bool initGraphics(FILE* logFile,
SDL_Window*& gameWindow,
// ^
SDL_Renderer*& gameRenderer,
// ^
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)

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.

Program shuts down immediately SDL

When I run this code (from the Lazy Foo SDL tutorial) the program immediately shuts down. Why is that? I'm sorry if it gets kind of messy because of the lack of comments, but I thought that it didn't really matter since there were comments on Lazy Foo's post. I get no errors when building it.
#include "SDL/SDL_image.h"
#include "SDL/SDL.h"
#include <string>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *load_image (std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface (int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface (source, NULL, destination, &offset);
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Event test", NULL);
return true;
}
bool load_files()
{
image = load_image ("background.png");
if (image == NULL)
{
return false;
}
return true;
}
void clean_up()
{
SDL_FreeSurface(image);
SDL_Quit();
}
int main(int argc, char* args[])
{
bool quit = false;
if (init() == false)
{
return 1;
}
if (load_files() == false)
{
return 1;
}
apply_surface(0,0, image, screen);
if(SDL_Flip(screen) == -1)
{
return 1;
}
while(quit == false)
{
while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}
If you're missing background.png, or any of the required DLL's in the executable directory, you can encounter weird crashes.
Being that this is one of the simpler programs in this series, I'd bet this is the issue. I've seen strange segfaults myself when I had a brain-fart and forgot a file, or it wasn't where the exe expected it to be.