Compiler can't find SDL headers? - c++

I downloaded the SDL .zip files and added the added the "include" files as necessary. When I went to run the example that had been pre-made by Code::Blocks, an error occured that said "SDL/SDL.h: No file or directory". Any help? Code below:
ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}

Related

SDL2 opens no window without giving any errors

I'm trying to test a simple SDL2 application. The app runs without any errors. However, no window shows up.
I'm using Ubuntu18.04 on a VMWare machine. I also tried to compile SDL from the source but got the same result. I am unsure if the cause of the problem is related to my OS or because of executing from a virtual machine.
Here is a simple application from docs (I just added SDL_GetNumDisplayModes to make sure the display mode is OK- returns 1)
// Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
// Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char *args[])
{
// The window we'll be rendering to
SDL_Window *window = NULL;
// The surface contained by the window
SDL_Surface *screenSurface = NULL;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
int res = SDL_GetNumDisplayModes(0);
printf("SDL_GetNumDisplayModes: %d\r\n", res);
// Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface(window);
// Fill the surface white
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
// Update the surface
SDL_UpdateWindowSurface(window);
// Hack to get window to stay up
SDL_Event e;
bool quit = false;
while (quit == false)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
}
}
}
// Destroy window
SDL_DestroyWindow(window);
// Quit SDL subsystems
SDL_Quit();
return 0;
}

How can I handle input in SDL?

My SDL program wont work. It was supposed to change the image when I pressed Up. However, it changes the image only when I click on the x in the Window
#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
int main(int argc, char* argv[])
{
enum KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_TOTAL
};
SDL_Init(SDL_INIT_VIDEO);
SDL_Event e;
SDL_Window* window = SDL_CreateWindow("antena1", // window's title
10, 25, // coordinates on the screen, in pixels, of the window's upper left corner
640, 420, // window's length and height in pixels
SDL_WINDOW_OPENGL);
SDL_Surface* key_press_surface[KEY_PRESS_SURFACE_TOTAL];
SDL_Surface* gImage = NULL;
SDL_Surface* gScreenSurface = NULL;
bool quit = false;
key_press_surface[KEY_PRESS_SURFACE_UP] = SDL_LoadBMP("hello_world.bmp");
gScreenSurface = SDL_GetWindowSurface(window);
gImage = SDL_LoadBMP("nick.bmp");
if (gImage == NULL) {
printf("Erro", SDL_GetError);
}
SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(window);
gScreenSurface = SDL_GetWindowSurface(window);
while (!quit) {
while (SDL_PollEvent(&e) == 0) {
if (e.type == SDL_QUIT) {
quit = true;
//SDL_DestroyWindow(window);#
}
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_LEFT:
gImage = key_press_surface[KEY_PRESS_SURFACE_UP];
break;
default:
gScreenSurface = NULL;
break;
}
}
}
};
SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(30000);
return 0;
}
https://wiki.libsdl.org/SDL_PollEvent
SDL_PollEvent returns 0 if there are no events available. So per the example in the above article, in order to get into the while statement for SDL_PollEvent, there must be events in the queue. However, your code only goes into the loop if there are NO events in the queue. So anything that happens once you get there is undefined.
IOW, just remove the "== 0".

SDL_BlitSurface doesn't draw to screen

I'm trying to simply create a window and blit an image to the screen but for some reason the image is not showing up and the screen remains white. I've been using lazyfoo's tutorial for reference in order to figure this out. I posted all my code below, hopefully it's readable enough. Help would be appreciated as I've been stuck on this for an hour or two, thank you.
#include <SDL.h>
#include <SDL_audio.h>
#include <cstdio>
#include <iostream>
#include <Windows.h>
// creates Load_surface function
SDL_Surface *loadSurface(std::string 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 ** argv)
{
// pointer variable for window
SDL_Window *window;
SDL_Surface *surface = NULL;
SDL_Surface *media =
loadSurface("C:\\Users\\nickl\\Desktop\\buttonpressed.bmp");
// initialize and test for failure
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) {
return 1;
}
// create the window
window = SDL_CreateWindow(
"Ice Cream Boys Software",
48,
48,
640,
480,
SDL_WINDOW_OPENGL
);
// safety check, see if window was created, otherwise throw an error
if (window = NULL) {
printf("Could not create window: %s", SDL_GetError());
return 1;
}
else {
OutputDebugString(TEXT("Surface loaded.\n"));
surface = SDL_GetWindowSurface(window);
}
SDL_BlitSurface(media, NULL, surface, NULL);
SDL_UpdateWindowSurface(window);
/* ************************************** loop **************************** */
// bool to keep window open and event handler
bool quit = false;
SDL_Event e;
// loop to keep window open
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_KEYDOWN) {
OutputDebugString(TEXT("My output string.\n"));
}
}
}
/* ************************************************************************** */
// close and destroy window
SDL_DestroyWindow(window);
// quit
SDL_Quit();
return 0;
}

Moving Bitmaps in SDL

My image cant load can someone tell me why please?
I'm trying to change the offset when I press WASD, it will work in Code::Blocks but not the .exe file.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
int WIDTH = 800;
int HEIGHT = 600;
int main (int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE|SDL_DOUBLEBUF);
if (!screen){
printf("Unable to set 800x600 video: %s\n", SDL_GetError());
return 1;
}
SDL_WM_SetCaption("The Killer Of The Night Pre-Release 0.0.1", NULL);
SDL_Surface *player = SDL_LoadBMP("lol.bmp");
if (!player){
printf("Unable to load the image here's the error: %s\n", SDL_GetError());
}
SDL_Rect offset;
offset.x = 100;
offset.y = 200;
bool done = false;
while (!done)
{
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
done = true;
}
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = true;
break;
case SDLK_w:
offset.y -= 1;
break;
case SDLK_a:
offset.x -= 1;
break;
case SDLK_s:
offset.y += 1;
break;
case SDLK_d:
offset.x += 1;
break;
}
}
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 25,25,255));
SDL_BlitSurface(player, 0, screen, &offset);
SDL_Flip(screen);
}
return 0;
}
It's working fine in Code::Blocks but when I run the .exe file it's not working!?!
You need to put lol.bmp in the directory with your exe.
When you run an executable in CodeBlocks, the working directory is the directory of the Code Block project file (.cbp), which is where your lol.bmp is located. You need to put that file in the bin/Debug/ folder or bin/Release/ folder depending on your build target.
More info would be nice since "it's not working" doesn't really say what's failing. For example is it a dll error, or like previously stated is lol.bmp not in the proper folder. Also I believe SDL_Rect needs a height and width as well.

Why Is SDL_image not working

I am new to C++ and SDL; I am trying to add a new SDL extension libary using instructions found here: http://www.lazyfoo.net/SDL_tutorials/lesson03/windows/devcpp/index.php
But I get these errors:
3 C:\Documents and Settings\Edmund\My Documents\C++\myprojects\SDL\SDLevent.cpp SDL/SDL_image.h: No such file or directory.
C:\Documents and Settings\Edmund\My Documents\C++\myprojects\SDL\SDLevent.cpp In function `SDL_Surface* load_image(std::string)':
28 C:\Documents and Settings\Edmund\My Documents\C++\myprojects\SDL\SDLevent.cpp `IMG_Load' undeclared (first use this function)
and then a bunch of unqualified ids.
This is my code:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
//The event structure that will be used
SDL_Event event;
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;
//Get the offsets
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Event test", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the image
image = load_image( "astyle.bmp" );
//If there was an error in loading the image
if( image == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the image
SDL_FreeSurface( image );
//Quit SDL
SDL_Quit();
}
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Apply the surface to the screen
apply_surface( 0, 0, image, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
}
//Free the surface and quit SDL
clean_up();
return 0;
}
It is pretty much identical to what is on the tutorial so it should not be a problem with the code. I have followed the Instructions on Lazy foo to the letter, I have put all of the files in the right place and linked to them so i do not know what I am doing wrong.
Your compiler can't find the SDL/SDL_image.h header, that leads to all those 'undeclared' errors.
Maybe you skipped Step 2 in the linked instructions.
Are you sure you have the SDL_Image function at all? What IDE are you using?
If its visual studio make sure you have linked the following:
Also make sure you have downloaded the latest SDL_Image files from http://www.libsdl.org/projects/SDL_image/
Another possible issue is that you havent placed the SDL_Image DLL files in the correct place.
you should use
#include "SDL.h"
#include "SDL_image.h"
and make sure that you have put the sdl include folder in your include directory