I'm going through the tutorial on Lazyfoo: 'Beginning Game Programming'. I've just completed tutorial number 4.
My problem is as follows:
The code works fine apart from the line:
SDL_Surface* loadSurface( std::string path );
The error reads:
error: expected ')' before ':' token
I've come to the conclusion that the error might have something to do with the headers. It's possible I should add something to the SDL.h header.
I also added the stdbool.h header to fix a separate problem. I wonder if that has caused issues.
Here's the full code, which is just the tutorial code (EDIT: I've put the problematic line in bold)
(or at least those stars have gone around it. Doesn't appear to be bolding within the code. It's near the beginning, line 33):
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Key press surfaces constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image
**SDL_Surface* loadSurface( std::string path );**
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ];
//Current displayed image
SDL_Surface* gCurrentSurface = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load default surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] = loadSurface( "04_key_presses/press.bmp" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] == NULL )
{
printf( "Failed to load default image!\n" );
success = false;
}
//Load up surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] = loadSurface( "04_key_presses/up.bmp" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] == NULL )
{
printf( "Failed to load up image!\n" );
success = false;
}
//Load down surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] = loadSurface( "04_key_presses/down.bmp" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] == NULL )
{
printf( "failed to load down image!\n" );
success = false;
}
//Load left surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] = loadSurface( "04_key_presses/left.bmp" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL )
{
printf( "failed to load left image!\n" );
success = false;
}
//Load right surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] = loadSurface( "04_key_presses/right.bmp" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL )
{
printf( "Failed to load left image!\n" );
success = false;
}
return success;
}
void close()
{
int i;
//Deallocate surface
for( i < KEY_PRESS_SURFACE_TOTAL; ++i; )
{
SDL_FreeSurface( gKeyPressSurfaces[ i ] );
gKeyPressSurfaces[ i ] = NULL;
}
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
SDL_Surface* loadSurface( std::string path )
{
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
return loadedSurface;
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//Set default current surface
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//User presses a key
else if( e.type == SDL_KEYDOWN )
{
//Select surfaces based on key press
switch( e.key.keysym.sym )
{
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ];
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
break;
default:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
break;
}
}
}
//Apply the current image
SDL_BlitSurface( gCurrentSurface, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
}
}
//Free resources and close SDL
close();
return 0;
}
Try:
#include <string>
instead of:
#include <string.h>
As already pointer out, you should try including C++'s string.
#include <string>
However, if that's your problem, the compiler should have said string is not in the std namespace.
To me it looks like the compiler doesn't know about the :: namespace operator.
The possible cause is that you're using a C compiler instead of a C++ compiler.
C doesn't have a notion of namespace, and it has no std::string.
Make sure your source file's extension is a C++ one (like .cpp) as opposed to C (.c).
Depending on your compiler, you might need to tell it you mean C++ and not C.
If you're using gcc, try g++ instead.
Related
I'm trying to put an imagine on surface.
So it's showed a black screen when I'm load BMP and display bmp
here's my code:
#define SDL_MAIN_HANDLED
#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool good = true;
if(SDL_Init(SDL_INIT_VIDEO)<0){
std::cout<<"ERROR: Failed to initialize SDL " << SDL_GetError()<<"\n";
good = false;
}else{
//Create Window
window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
if (window ==NULL){
std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
good = false;
}else{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
}
}
return good;
}
void close(){
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(window);
window=NULL;
SDL_Quit();
}
bool loadMedia(){
bool ok = true;
gHelloWorld=SDL_LoadBMP("hello_world.bmp");
if(gHelloWorld==NULL){
std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
ok = false;
}else{
std::cout<<"Loaded successfully "<<'\n';
}
return ok;
}
int main(){
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
bool Init = init();
if(Init){
bool loadMed = loadMedia();
if(loadMed){
SDL_Rect area ={30,30,250,250};
SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
SDL_UpdateWindowSurface(window); //
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
close();
return 0;
}
And I'm using Cmake to link lib (dynamic linking)
I tried to write SDL_Rect , and its not work ;/
Your problem is that you are nulling your sufaces in the main function.
Here is a version of your code without that mistake:
#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;
SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool good = true;
if(SDL_Init(SDL_INIT_VIDEO)<0){
std::cout<<"ERROR: Failed to initialize SDL " << SDL_GetError()<<"\n";
good = false;
}else{
//Create Window
window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
if (window ==NULL){
std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
good = false;
}else{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
}
}
return good;
}
void close(){
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(window);
window=NULL;
SDL_Quit();
}
bool loadMedia(){
bool ok = true;
gHelloWorld=SDL_LoadBMP("/tmp/image.bmp");
if(gHelloWorld==NULL){
std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
ok = false;
}else{
std::cout<<"Loaded successfully "<<'\n';
}
return ok;
}
int main(){
// your error was nulling your surfaces.
// SDL_Window* window=NULL;
// SDL_Surface* screenSurface=NULL;
bool Init = init();
if(Init){
bool loadMed = loadMedia();
if(loadMed){
SDL_Rect area ={30,30,250,250};
SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
SDL_UpdateWindowSurface(window); //
SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
}
}
close();
return 0;
}
Also, I recommend that you make your surface updates inside your while loop because somethimes SDL don't render the first surface and you obta a false window.
Here is a good resource for learning SDL: https://lazyfoo.net/tutorials/SDL/
and on my GitHub I have a tool to give you an SDL example: https://github.com/Monsterduty/mktest.
I've been going back over some SDL tutorials I did a while back to practice pointers/references, but I got stuck on something.
If I declare the SDL_Renderer globally everything works, but if I try to declare it in main, and pass the renderer to the functions that need it, I eventually get an SDL_Error saying "invalid renderer".
What works:
...
// Global vars
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Rect gSpriteClips[ 4 ];
LTexture gSpriteSheetTexture;
...
// called from main after SDL initialization
bool loadMedia()
{
bool success = true;
//Load sprite sheet texture
if( !gSpriteSheetTexture.loadFromFile( gRenderer, "img/dots.png" ) )
{
printf( "Failed to load sprite sheet texture!\n" );
success = false;
}
...
What doesn't work:
...
bool init( SDL_Window* window, SDL_Renderer* renderer, const int SCREEN_WIDTH, const int SCREEN_HEIGHT )
{
...
}
...
bool load_media( SDL_Renderer* renderer, LTexture& sprite_sheet_texture, SDL_Rect* sprite_clips )
{
bool success = true;
//Load sprite sheet texture
if( !sprite_sheet_texture.loadFromFile( renderer, "img/dots.png" ) )
{
printf( "Failed to load sprite sheet texture!\n" );
success = false;
}
...
int main(int argc, char* args[])
{
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Rect sprite_clips[ 4 ];
LTexture sprite_sheet_texture;
if( !init( window, renderer, SCREEN_WIDTH, SCREEN_HEIGHT ) )
{
std::cout << "Failed to initialize!\n";
}
else
{
if( !load_media( renderer, sprite_sheet_texture, sprite_clips ) )
{
std::cout << "Failed to load media!\n";
}
...
The texture class: (same file for both versions)
bool LTexture::loadFromFile( SDL_Renderer* gRenderer, const std::string &path )
{
// Get rid of preexisting texture
free();
// The final texture
SDL_Texture* newTexture = NULL;
// Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
std::cout << "Unable to load image! SDL_Image error: " << IMG_GetError() << std::endl;
}
else
{
// color key image
SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
// Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface ); <-- POINT OF FAILURE
if( newTexture == NULL )
{
std::cout << "Unable to create texture! SDL error: " << SDL_GetError() << std::endl; <-- THE ERROR I GET
}
...
The way I see it, SDL_CreateTextureFromSurface expects a pointer to an SDL_Renderer, which is what I'm passing. I don't understand why it matters if it's globally declared or declared in main. It should still just be a chunk of memory I'm pointing to somewhere.
I understand I must be passing it wrong, but I can't figure out how to do it correctly, and everything I've tried has produced the same error (passing the pointer as a reference, as a pointer to the pointer, etc.).
Side-note: I know I could just declare it globally or create a Singleton and be done with it, but this is more a learning exercise for me - so the point is to understand the pointer/reference aspect of the problem.
In the second snippet, if init changes the variable renderer, it will only change its local copy of the variable. It will not change the original variable renderer in the function main. This is because you are passing the variable by value. If you instead pass it by pointer or by reference, you will also modify the original variable's value. That way, your program will behave the same way as your global variable version.
Note that in order to pass a pointer to another function by pointer, you will need a pointer to a pointer. In your case you will need an SDL_Renderer **.
From the answer and advice by Andreas Wenzel, I now have a working solution. I thought I was passing by pointer, when I was actually passing by value. By passing a pointer to a pointer instead it works as intended.
I've included snippets below to show the difference from the code in the original question.
int main(int argc, char* args[])
{
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Rect sprite_clips[ 4 ];
LTexture sprite_sheet_texture;
if( !init( &window, &renderer, SCREEN_WIDTH, SCREEN_HEIGHT ) )
{
std::cout << "Failed to initialize!\n";
}
else
{
if( !load_media( &renderer, sprite_sheet_texture, sprite_clips ) )
{
std::cout << "Failed to load media!\n";
}
...
bool init( SDL_Window** window, SDL_Renderer** renderer, const int SCREEN_WIDTH, const int SCREEN_HEIGHT )
{
bool success = true;
// initialize SDL
if( SDL_Init( SDL_INIT_VIDEO) < 0 )
{
std::cout << "SDL could not initialize! SDL error: " << SDL_GetError() << std::endl;
success = false;
}
else
{
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
std::cout << "Warning: Linear texture filtering not enabled!\n";
}
// Create window
*window = SDL_CreateWindow(
"SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if( window == NULL )
{
std::cout << "Window could not be created! SDL error: " << SDL_GetError() << std::endl;
success = false;
}
else
{
// Create renderer for window
*renderer = SDL_CreateRenderer( *window, -1, SDL_RENDERER_ACCELERATED );
if( renderer == NULL )
{
std::cout << "Renderer could not be created! SDL error: " << SDL_GetError() << std::endl;
success = false;
}
...
bool load_media( SDL_Renderer** renderer, LTexture& sprite_sheet_texture, SDL_Rect* sprite_clips )
{
bool success = true;
//Load sprite sheet texture
if( !sprite_sheet_texture.loadFromFile( *renderer, "img/dots.png" ) )
{
printf( "Failed to load sprite sheet texture!\n" );
success = false;
}
...
void close( SDL_Window** window, SDL_Renderer** renderer )
{
//Destroy Window
SDL_DestroyRenderer( *renderer );
SDL_DestroyWindow( *window );
*window = NULL;
*renderer = NULL;
IMG_Quit();
SDL_Quit();
}
I have tried to write a code that creates a window which shows a particular image when pressed a particular arrow key and closes the window when the x button(on the screen) is clicked:
#include <SDL.h>
#include <iostream>
#include <string>
using namespace std;
enum Gamestate
{
Play, Exit
};
enum Keypress
{
KEY_PRESS_DEFAULT,
KEY_PRESS_UP,
KEY_PRESS_DOWN,
KEY_PRESS_RIGHT,
KEY_PRESS_LEFT,
KEY_PRESS_TOTAL,
};
class Game
{
public:
Game();
~Game();
void run();
private:
//data members
int WINDOW_WIDTH;
int WINDOW_HEIGHT;
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Surface* loadSurface[ KEY_PRESS_TOTAL ];
SDL_Surface* defaultSurface = NULL;
Gamestate _gamestate;
private:
//methods
void init();
void gameLoop();
void processInput();
void updateWindowSurface();
};
void error( string error1 )
{
int itq;
cout << error1 << endl;
SDL_GetError();
cout << endl << "Enter any key to quit" << endl;
cin >> itq;
SDL_Quit();
}
Game::Game()
{
WINDOW_WIDTH = 640;
WINDOW_HEIGHT = 480;
_gamestate = Play;
}
void Game::run()
{
init();
gameLoop();
}
void Game::init()
{
SDL_Init( SDL_INIT_EVERYTHING );
window = SDL_CreateWindow( "GAME", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
error( "SDL could not create a window!" );
}
screenSurface = SDL_GetWindowSurface( window );
if( window == NULL )
{
error( "SDL could not create a screen!" );
}
loadSurface[ KEY_PRESS_DEFAULT ] = SDL_LoadBMP( "press.bmp" );
if( loadSurface[ KEY_PRESS_DEFAULT ] == NULL )
{
error( "Unable to load the Surface!" );
}
loadSurface[ KEY_PRESS_UP ] = SDL_LoadBMP( "up.bmp" );
if( loadSurface[ KEY_PRESS_UP ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_DOWN ] = SDL_LoadBMP( "down.bmp" );
if( loadSurface[ KEY_PRESS_DOWN ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_RIGHT ] = SDL_LoadBMP( "right.bmp" );
if( loadSurface[ KEY_PRESS_RIGHT ] == NULL )
{
error( "Unable to load Image!" );
}
loadSurface[ KEY_PRESS_LEFT ] = SDL_LoadBMP( "left.bmp" );
if( loadSurface[ KEY_PRESS_LEFT ] == NULL )
{
error( "Unable to load Image!" );
}
defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
}
void Game::gameLoop()
{
while( _gamestate != Exit )
{
processInput();
updateWindowSurface();
}
}
void Game::processInput()
{
SDL_Event e;
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_QUIT )
{
_gamestate = Exit;
}
else if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
case SDLK_UP:
defaultSurface = loadSurface[ KEY_PRESS_UP ];
break;
case SDLK_DOWN:
defaultSurface = loadSurface[ KEY_PRESS_DOWN ];
break;
case SDLK_RIGHT:
defaultSurface = loadSurface[ KEY_PRESS_RIGHT ];
break;
case SDLK_LEFT:
defaultSurface = loadSurface[ KEY_PRESS_LEFT ];
break;
default:
defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
}
}
}
}
void Game::updateWindowSurface()
{
SDL_BlitSurface( defaultSurface, NULL, screenSurface, NULL );
SDL_UpdateWindowSurface( window );
}
Game::~Game()
{
for( int i = 0; i <= KEY_PRESS_TOTAL; i++ )
{
SDL_FreeSurface( loadSurface[ i ] );
loadSurface[ i ] = NULL;
}
SDL_DestroyWindow( window );
window = NULL;
SDL_Quit();
}
int main( int argc, char* args[] )
{
Game game;
game.run();
return 0;
}
When I press the arrow keys the images which display is correct but when I try to close it, the program crashes. But when I try closing the window before pressing any arrow keys it works just fine.
Can any one help me with it?
My Makefile -
all:
g++ main.cpp -I/usr/local/include -L/usr/local/lib -lSDL2 -lSDL_image
My Code I am using as taken from a tutorial -
/*This source code copyrighted by Lazy Foo' Productions (2004-2013) */
//Using SDL, SDL_image, standard IO, and strings
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture();
//Deallocates memory
~LTexture();
//Loads image at specified path
bool loadFromFile( std::string path );
#ifdef _SDL_TTF_H
//Creates image from font string
bool loadFromRenderedText( std::string textureText, SDL_Color textColor );
#endif
//Deallocates texture
void free();
//Set color modulation
void setColor( Uint8 red, Uint8 green, Uint8 blue );
//Set blending
void setBlendMode( SDL_BlendMode blending );
//Set alpha modulation
void setAlpha( Uint8 alpha );
//Renders texture at given point
void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );
//Gets image dimensions
int getWidth();
int getHeight();
private:
//The actual hardware texture
SDL_Texture* mTexture;
//Image dimensions
int mWidth;
int mHeight;
};
//The application time based timer
class LTimer
{
public:
//Initializes variables
LTimer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
Uint32 getTicks();
//Checks the status of the timer
bool isStarted();
bool isPaused();
private:
//The clock time when the timer started
Uint32 mStartTicks;
//The ticks stored when the timer was paused
Uint32 mPausedTicks;
//The timer status
bool mPaused;
bool mStarted;
};
//The dot that will move around on the screen
class Dot
{
public:
//The dimensions of the dot
static const int DOT_WIDTH = 20;
static const int DOT_HEIGHT = 20;
//Maximum axis velocity of the dot
static const int DOT_VEL = 10;
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handleEvent( SDL_Event& e );
//Moves the dot
void move();
//Shows the dot on the screen
void render();
private:
//The X and Y offsets of the dot
int mPosX, mPosY;
//The velocity of the dot
int mVelX, mVelY;
};
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
//Scene textures
LTexture gDotTexture;
LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
//Deallocate
free();
}
bool LTexture::loadFromFile( std::string path )
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else
{
//Color key image
SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
else
{
//Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
#ifdef _SDL_TTF_H
bool LTexture::loadFromRenderedText( std::string textureText, SDL_Color textColor )
{
//Get rid of preexisting texture
free();
//Render text surface
SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
if( textSurface != NULL )
{
//Create texture from surface pixels
mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
if( mTexture == NULL )
{
printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
}
else
{
//Get image dimensions
mWidth = textSurface->w;
mHeight = textSurface->h;
}
//Get rid of old surface
SDL_FreeSurface( textSurface );
}
else
{
printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
}
//Return success
return mTexture != NULL;
}
#endif
void LTexture::free()
{
//Free texture if it exists
if( mTexture != NULL )
{
SDL_DestroyTexture( mTexture );
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void LTexture::setColor( Uint8 red, Uint8 green, Uint8 blue )
{
//Modulate texture rgb
SDL_SetTextureColorMod( mTexture, red, green, blue );
}
void LTexture::setBlendMode( SDL_BlendMode blending )
{
//Set blending function
SDL_SetTextureBlendMode( mTexture, blending );
}
void LTexture::setAlpha( Uint8 alpha )
{
//Modulate texture alpha
SDL_SetTextureAlphaMod( mTexture, alpha );
}
void LTexture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, center, flip );
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}
Dot::Dot()
{
//Initialize the offsets
mPosX = 0;
mPosY = 0;
//Initialize the velocity
mVelX = 0;
mVelY = 0;
}
void Dot::handleEvent( SDL_Event& e )
{
//If a key was pressed
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
{
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_UP: mVelY -= DOT_VEL; break;
case SDLK_DOWN: mVelY += DOT_VEL; break;
case SDLK_LEFT: mVelX -= DOT_VEL; break;
case SDLK_RIGHT: mVelX += DOT_VEL; break;
}
}
//If a key was released
else if( e.type == SDL_KEYUP && e.key.repeat == 0 )
{
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_UP: mVelY += DOT_VEL; break;
case SDLK_DOWN: mVelY -= DOT_VEL; break;
case SDLK_LEFT: mVelX += DOT_VEL; break;
case SDLK_RIGHT: mVelX -= DOT_VEL; break;
}
}
}
void Dot::move()
{
//Move the dot left or right
mPosX += mVelX;
//If the dot went too far to the left or right
if( ( mPosX < 0 ) || ( mPosX + DOT_WIDTH > SCREEN_WIDTH ) )
{
//Move back
mPosX -= mVelX;
}
//Move the dot up or down
mPosY += mVelY;
//If the dot went too far up or down
if( ( mPosY < 0 ) || ( mPosY + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
//Move back
mPosY -= mVelY;
}
}
void Dot::render()
{
//Show the dot
gDotTexture.render( mPosX, mPosY );
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Enable VSync
if( !SDL_SetHint( SDL_HINT_RENDER_VSYNC, "1" ) )
{
printf( "Warning: VSync not enabled!" );
}
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load dot texture
if( !gDotTexture.loadFromFile( "26_motion/dot.bmp" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
return success;
}
void close()
{
//Free loaded images
gDotTexture.free();
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//The dot that will be moving around on the screen
Dot dot;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle input for the dot
dot.handleEvent( e );
}
//Move the dot
dot.move();
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render objects
dot.render();
//Update screen
SDL_RenderPresent( gRenderer );
}
}
}
//Free resources and close SDL
close();
return 0;
}
My Error -
g++ main.cpp -I/usr/local/include -L/usr/local/lib -lSDL2 -lSDL_image.lib
ld: library not found for -lSDL_image.lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
I am unable to get this to load. I used brew to install SDL2_image and it appears to exist in usr/local/lib and such. I see libSDL_image.a and other assorted items.
Can someone please tell me how to edit my makefile for mac so that I can compile the code I am attempting to use. I am not sure why -lSDL_image would not work as it should do so.
To load SDL_image it should be
-lSDL_image
but your not using SDL 1.X your are using SDL 2 so it should be
-lSDL2_image
If it still says that it can't find it then you need to make sure it is in a path that ld searches in.
-L/Path/To/SDL_image
If that still doesn't work make sure that you have the development library and not just the runtime binaries
You can grab the development libraries for SDL_image here : http://www.libsdl.org/projects/SDL_image/
#include "SDL/SDL.h"
#include "SDL/SDL_Image.h"
#include <string>
using namespace std;
//Const screen variables
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const char* SCREEN_CAPTION = "Pyro";
const float GRAVITY = 9.8; //Metres per second **NEEDS TO BE ADJUSTED BECAUSE IT'S IN METRES, NOT PIXELS**
const int jumpHeight = 10;
//Non-Const variables
bool running = true;
bool isJumping = true;
int jump = 0;
int frame = 0;
int level = 1;
SDL_Event event;
Uint8 *keystate = NULL;
//SDL Surfaces
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *bEnemy[10];
//Structs
typedef struct entity {
int health;
int damage;
SDL_Rect hitbox;
bool evolved;
} playerType, enemyType;
playerType player;
enemyType basicEnemy[10];
//Functions
SDL_Surface *loadImage( std::string filename )
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void applySurface( int x, int y, SDL_Surface* source, SDL_Surface* location )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, location, &offset );
}
//Main Function
int main( int argc, char* argv[] )
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL )
{
return 1;
}
SDL_WM_SetCaption( SCREEN_CAPTION, NULL );
background = loadImage( "images/background.png" );
sprite = loadImage( "images/player.png" );
SDL_FreeSurface( sprite );
SDL_FreeSurface( background );
while( running )
{
//Main game loop
if( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_q:
running = false;
break;
}
break;
}
}
keystate = SDL_GetKeyState(NULL);
if( keystate[SDLK_q] ) player.evolved = !player.evolved;
if( keystate[SDLK_UP] )
{
if(isJumping != true)
{
isJumping = true;
}
}
if( keystate[SDLK_LEFT] ) player.hitbox.x -= 1;
if( keystate[SDLK_RIGHT] ) player.hitbox.y += 1;
//Window collision
if( player.hitbox.x < 0 ) {player.hitbox.x = 0;}
else if( player.hitbox.x > SCREEN_WIDTH - player.hitbox.w ) {player.hitbox.x = SCREEN_WIDTH - player.hitbox.w;}
if( player.hitbox.y < 0 ) {player.hitbox.y = 0;}
else if( player.hitbox.y > SCREEN_HEIGHT - player.hitbox.h ) {player.hitbox.y = SCREEN_HEIGHT - player.hitbox.h;}
//Jumping
if( isJumping == true )
{
if(jump >= jumpHeight)
{
jump--;
player.hitbox.y++;
}else {
jump++;
player.hitbox.y--;
}
}
//Updating the screen
applySurface( 0, 0, background, screen );
applySurface( player.hitbox.x, player.hitbox.y, sprite, screen );
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
SDL_Quit();
return 0;
}
^ That is the exact code I have. When I run the file it immediately closes. It is compiled with: g++ -o myprogram.exe mysource.cpp -lmingw32 -lSDLmain -lSDL -lSDL_image -static-libgcc -static-libstdc++.
The files I have linked to do exist; they are currently placeholders (background is some random png image I found, and the player is an image of 8-bit mario).
How do I stop my program from closing immediately?
SDL_FreeSurface( sprite );
SDL_FreeSurface( background );
This is where your problem lies.
These lines should appear at the end of the program right before you call SDL_Quit().
Currently, you're blitting freed surfaces onto the window.