'SDL_SetVideoMode': identifier not found - c++

I have a problem with SDL lib. I'm using VS2012 Ultimate and i was actually using this tutorial: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php to set everything and i did it step by step few times, but I still have problems this is my code, very simple:
#include <iostream>
#include <SDL.h>
SDL_Surface * ekran = NULL;
int main (int argc, char *args [] )
{
SDL_Init( SDL_INIT_EVERYTHING );
ekran = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
SDL_Flip( ekran );
SDL_Delay( 2000 );
SDL_Quit();
return 0;
}
and im having this errors:
error C3861: 'SDL_SetVideoMode': identifier not found
error C3861: 'SDL_Flip': identifier not found

Here below is an example how to replace SDL_SetVideoMode() in SDL2. The old way to init SDL is commented and left along with the new way for comparison purposes. Basically, SDL2 creates a window with a title, then a surface attached to it, while SDL1 creates a surface alone and then calls the window manager to give it a name.
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL video init failed: %s\n", SDL_GetError());
return 1;
}
// SDL_Surface *screenSurface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
window = SDL_CreateWindow("Sphere Rendering",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
if (!screenSurface) {
fprintf(stderr, "Screen surface could not be created: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// SDL_WM_SetCaption("Sphere Rendering", NULL);

Take a look at that tutorial page again. Your code does not match it (e.g. SDL_SetVideoMode() no longer exists). Your code uses SDL 1.2 and the (updated) tutorial uses SDL 2.0. Are you using an old cached version of that page?

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;
}

sdl_loadbmp from file without using entire file path

I'm trying to load an image from file without using the entire file path. Like in C#, However everything I try just throws my sdl error in the console when it gets to loading the image.
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "SDL2_tutorials02/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "SDL2_tutorials02/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
This is part of my original code that I learned from an online tutorial, trying to teach myself SDL and more about c++
edit for InternetAussie, this is my entire source code from what I think I have learned from the tutorial is below:
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//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 surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = 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 splash image
gHelloWorld = SDL_LoadBMP( "SDL2_tutorials02/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "SDL2_tutorials02/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
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
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//system halt for testing
system("pause");
//Free resources and close SDL
close();
return 0;
}
and if it helps I followed the instructions in tutorial 1 at Lazy Foo' Productions. The hyperlink that I hope I have appropriately added is just the setup part after clicking through the steps on how to setup sdl on a windows machine using visual studios 2010 ultimate. I went with the system 32 setup using the console as my output source for errors.
As InternetAussie already said if you are running the compiled executable all the paths are relative to the directory your executable runs from.
What your problem seems to be is related to your IDE. When you build and run code from your IDE the working directory is taken from somewhere else. It can be some default path where your IDE builds debug versions of your program. It's hard to tell exactly because it's different for all of them.
If you want to debug your SDL project you should set the working directory for compiler to the directory you are compiling your executable into and where you also have your files you want to read saved.
F. e. : if you are using visual studio you can set this by right clicking on a project > settings > debugging > working path option or if you build your project with cmake you can also set it with SET_PROPERTY(TARGET Project_name PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "path").
If this didn't solve your problem, feel free to ask questions. Also if you are using cmake and VS, I can provide you with a complete cmakelists.txt that generates SDL2 project for VisualStudio and set's up all required settings.

X Error of failed request: BadValue (integer parameter out of range for operation)

Maybe it's just me being dumb, but I'm sure this should work.
#include <SDL2/SDL.h>
#include <GL/glew.h>
struct Display
{
SDL_Window* window;
SDL_GLContext context;
};
Display* init()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK , SDL_GL_CONTEXT_PROFILE_CORE );
SDL_Window* window = SDL_CreateWindow( "Ice Engine",
800, 600,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOW_OPENGL );
if ( !window )
{
printf( "%s\n", SDL_GetError() );
return nullptr;
}
SDL_GLContext context = SDL_GL_CreateContext( window );
if ( !context )
{
printf( "%s\n", SDL_GetError() );
return nullptr;
}
glewExperimental = GL_TRUE;
if ( glewInit() != GLEW_OK )
return nullptr;
return new Display{ window, context };
}
int main( int argc, char** argv )
{
Display* display = init();
bool running = true;
SDL_Event e;
while( running )
{
while( SDL_PollEvent( &e ) )
if ( e.type == SDL_QUIT )
running = false;
SDL_GL_SwapWindow( display->window );
}
delete display;
SDL_Quit();
}
I probably shouldn't be using new and delete and such, but this was just a quick setup to get my project going. The problem is it compiles just fine, but when I run it I get this error:
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 1 (X_CreateWindow)
Value in failed request: 0x0
Serial number of failed request: 155
Current serial number in output stream: 168
I've tried without setting the OpenGL context versions, but I just get the same error.
I tried switching to GLFW3 and it all works just fine. It creates me a window and a OpenGL 3.3 core profile context. So it seems to be a problem with SDL2. I'm running ubuntu 15.10 and I installed it via the command line with: sudo apt install libsdl2-dev.
You are calling SDL_CreateWindow incorrectly. You have mixed up the x, y and width, height settings. The right way would be:
SDL_Window* window = SDL_CreateWindow("Ice Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
SDL_WINDOW_OPENGL );
See SDL_CreateWindow reference. Other than that, your code looks fine.

SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface

Alrighty, from what I have researched, it appears that the Invalid Renderer error applies to a variety of cases and I'm lost onto why my code is creating it.
I have narrowed it down to a specific area of code
//If existing texture is there, free's and sets to NULL. Along with iWidth & iHeight = 0;
freetexture();
//final image texture
SDL_Texture* niTexture = NULL;
//Loads 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
{
printf("SpriteSheet :: Loaded\n");
Init mkey;
//Color key DOUBLE CHECK IF ERROR CHANGE TO ORIGINAL 0, 0xFF, 0xFF
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 50, 96, 166));
//create texture from surface pixels
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
if (niTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
Specifically at the line,
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
is causing SDL to return an Invalid renderer error. In my init class, the renderer initializes perfectly, only when I attempt to use it to load an image am I getting the Invalid Renderer error. Any help on how to fix this error is appreciated.
Edit::
Here's some code from the Init Class relating to the renderer,
printf("Linear Texture Filtering :: Enabled\n");
//Create Window
Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sw, sh, SDL_WINDOW_SHOWN);
if (Window == NULL)
{
printf("Window failed to be created\n");
SDLSuccess = false;
}
else
{
printf("Window :: Created\n");
//Create VYNC'd renderer for the window
Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (Renderer == NULL)
{
printf("Renderer failed to be created\n");
SDLSuccess = false;
}
Hope this helps with finding the issue.
It looks like your Renderer isn't initialized, unless the code you posted is in the constructor of your Init class.
Do you already have an instance of Init somewhere in your code that you mean to be referencing in your texture method? Check the value of your Renderer before you try to use it, something like:
if (mkey.Renderer) {
niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
if (niTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
} else {
printf("Renderer is not initialized");
}

Why won't my image render?

So I'm learning C++ with SDL (on Visual Studio 2013). I'm following Lazy Foo's tutorials (specifically: http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php).
My image won't render. In contrast to the tutorial (which works) I do not have global surface variables. Instead I declared them in the main and passed the pointers around.
Code:
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;
//SDL init.
bool initialiseSDL(SDL_Window* gWindow, SDL_Surface* gWindowSurface)
{
bool success = true;
/*SDL_Init() initisalises SDL library. Returning 0 or less signifies an error.
SDL_INIT_VIDEO flag refers to graphics sub system.*/
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialise. SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create the window
gWindow = SDL_CreateWindow("Asteroids", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Could not create window. SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get the window surface.
gWindowSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia(SDL_Surface* surface, std::string path)
{
//Success flag
bool success = true;
surface = SDL_LoadBMP(path.c_str());
if (surface == NULL)
{
printf("SDL surface failed to load. SDL Error: %s\n", SDL_GetError());
success = false;
}
return success;
}
void close()
{
}
int main(int argc, char* argv[])
{
SDL_Window* gWindow = NULL;
SDL_Surface* gWindowSurface = NULL;
SDL_Surface* gImageSurface = NULL;
if (!initialiseSDL(gWindow, gWindowSurface))
{
printf("Failed to initialise.\n");
}
else
{
if (!loadMedia(gImageSurface, "hw.bmp"))
{
printf("Failed to load inital media.");
}
else
{
//Apply the image
SDL_BlitSurface(gImageSurface, NULL, gWindowSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
//Wait two seconds
SDL_Delay(2000);
}
}
return 0;
}
Your program exhibits undefined behavior, because the pointers inside the main function are not initialized.
When you pass them to the initialiseSDL function you pass them by value, meaning they are copied and the function only operates on the copies and not the original pointers, which will still be uninitialized after the call.
What you need to do is to pass the pointers by reference instead:
bool initialiseSDL(SDL_Window*& gWindow, SDL_Surface*& gWindowSurface)
You of course need to do the same with the loadMedia function.