i am trying to learn SDL2 from lazyfoo.net and i have quite a few problems (i've also been learning c++ since January so i do know some of what is going on, but not all of it), the first and most obvious one is that the "Hello world" appears only when i start without debugging, if i do debug it gives me "the program can't start because SDL2.dll is missing from your computer". which is not true at all. i put the .dll file into my current project's folder (which i have called TEST), the site just told me to put it in the same area as the .vxcproj file is, which is what i did...
also the tutorial tells me to type #include <SDL.h>, and when i do it says a lot of the stuff is undefined (i just copied and pasted this directly from the zip file at the bottom of the tutorial page), so i have to use #include <SDL2/SDL.h>
and the third one (that i managed to fix, sorta) is when putting the image onto the screen, it showed that it could not find the .bmp file, so i had to get the file into the source files section of visual studio. it works for this test but if i start incorporating multiple .bmp files it might become confusing. was i supposed to do that?
here is my code, which is almost identical to the one in the zip file
#include <SDL2/SDL.h> //first modification i did
#include <stdio.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool init();
bool loadMedia();
void close();
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;
bool init()
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
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
{
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia()
{
bool success = true;
gHelloWorld = SDL_LoadBMP("hello_world.bmp"); //second modification
if (gHelloWorld == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError());
success = false;
}
return success;
}
void close()
{
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
int main(int argc, char* args[])
{
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
SDL_Delay(2000);
}
}
close();
return 0;
}
HMMM
Your questions:
1
I'm also bothered, it would be better to use #including , and it would be better if you put those stuffs into the header file like stdafx or the header of your class to make it less confusing, there's a big chance you messed up on integrating your directories + your .dlls are not in a good place.
2
You're supposed to put all the dlls into the main project's folder's debug, the area where you see an exe, .pdb, and .ilk.
3
You used "hello_world.bmp", there's nothing wrong with that, it's your file's location, if you're going to run debug then place the files (the .bmp) inside... let's say you made a project named ABC, and placed it in a folder called ABC, there would be 3 folders called DEBUG (if you ran debug at least once), ipch, and ABC.
you're supposed to place the files inside the ABC folder which contains the .vcxproj, and other stuffs, then if you wanted to run your game using the .exe file in the mainfolder's debug folder then you're supposed to copy all the files you used (in this case, you used the .bmp) and paste them in that folder, the folder that contains the .exe, that way if you run debug, it would read in the ABC folder while if you personally go to the .exe file, it would read the files that are with it in it's folder.
#Skalli he clearly stated Visual Studio which means he is running Windows,
please when incorporating images, place them at the same directory as the exe you built when using Release mode. Look at the SDL library files, go to bin and copy the SDL2.dll and place it where your EXE is. Also to be able to run the program from the IDE, go to your Program Files x86 folder and go to your IDE folder, should be called "Microsoft Visual Studio XX" and go to the bin and paste the SDL2.dll
comment on my answer if the problem persists
Related
Im trying to play around with SDL2 following lazyfoo's tutorials to get accustomed to it, but even the basic most program doesn't work properly. I can open a basic blank window with no image and keep it open, but as soon as I try to open a BMP file in a window, it all acts weird and doesn't work anymore. My code, that initially shows no errors:
#include <SDL2/SDL.h>
#include <cstdio>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *newWindow = nullptr;
SDL_Surface *loadedImage = nullptr;
SDL_Surface *screenSurface = nullptr;
bool quit = false;
SDL_Event event;
bool initWindow() {
bool state = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::puts("Error init");
state = false;
}
else
{
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOWEVENT_SHOWN);
if (nullptr == newWindow)
{
std::puts("Error window");
state = false;
}
else
{
screenSurface = SDL_GetWindowSurface(newWindow);
}
}
return state;
}
bool loadMedia() {
bool success = true;
loadedImage = SDL_LoadBMP("LAND3.BMP");
if (loadedImage == nullptr)
{
printf("Error image %s \n", SDL_GetError());
success = false;
}
return success;
}
void closeWindow() {
SDL_FreeSurface(loadedImage);
loadedImage = nullptr;
SDL_DestroyWindow(newWindow);
newWindow = nullptr;
SDL_Quit();
}
int main(int argc, char *args[]) {
if (!initWindow())
{
std::puts("Error init main");
}
else
{
if (!loadMedia())
{
std::puts("Error image main");
}
else
{
while (!quit)
{
if (event.type == SDL_QUIT)
{
quit = true;
}
else
{
SDL_BlitSurface(loadedImage, nullptr, screenSurface, nullptr);
SDL_UpdateWindowSurface(newWindow);
}
}
}
}
closeWindow();
return 0;
}
When running this program, I get no errors but the UI starts acting all crazy; resolution gets very small(way smaller than 480p set up by me), all windows resize and this lasts for a brief period. If I replace the while(!quit) loop with a SDL_Delay(1000), this behaviour lasts approximately as long as the delay.
Initially my suspicion was that the file I was using the first time was corrupted(I had just renamed an existing picture), but then I downloaded a sample BMP file and nothing changed.
When using the debugger I get an error from loadMedia() that the file could not be loaded, regardless of which one I use. I am using MinGW and cLion.
What might be the issue?
The constant SDL_WINDOWEVENT_SHOWN is not a valid flag for the function SDL_CreateWindow. That constant is not intended as a flag, but as an event ID. You probably meant to use the constant SDL_WINDOW_SHOWN instead.
The constant SDL_WINDOWEVENT_SHOWN happens to have the same value as SDL_WINDOW_FULLSCREEN (both have the value 1). Therefore, your incorrect function call
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOWEVENT_SHOWN);
is equivalent to:
newWindow = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN);
In other words, you are unintentionally asking SDL to create the window in 640*480 fullscreen mode. That is probably the reason for your desktop being resized.
The reason for the file not being found is probably because the file is not in your program's current working directory. You can either ensure that the file is in that directory or you can use an absolute path to the file, for example "C:\\Users\\MyUsername\\Desktop\\LAND3.BMP".
I doubt that your problem of not being able to kill your application using the task manager is the fault of SDL. It is probably a design flaw of Microsoft Windows. See this link on how to make the task manager more accessible from a hung fullscreen application.
This test program should create a blank window that stays open until you x-it-out. I copied it from SDL's documentation to make sure it is correct. It can be found here.
// Example program:
// Using SDL2 to create an application window
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
//game loop, quitGame to quit
bool quitGame = false;
//var for checking events
SDL_Event event;
while(!quitGame) {
//Update particles
//Draw particles
//Check for events
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
quitGame = true;
}
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
It doesn't create a window and terminates immediately, but gives no errors.
I'm using Eclipse, mingw32, and the latest stable release of SDL2. SDL2's libraries and headers are within a file in my C drive. I am using a 64 bit system. I include the entire folder of SDL2's header files. The only library folder I have linked is the one within the 64 bit part of the SDL2 folder. The libraries I have linked are the ones suggested by HolyBlackCat, (in this order) mingw32, SDL2main, and SDL2. Any help is greatly appreciated. Thanks!
I'm new to c++ and XCode, I'm using sdl2 to create a window but when i compile it, it crashes giving me a thread.I have included opengl.h , stdio.h and SDL2.h. There are questions about
dlyd:library not loaded but their different.
Error Message:
dyld: Library not loaded: #rpath/SDL2.framework/Versions/A/SDL2 Referenced from:
/Users/shayanrazavi/Library/Developer/Xcode/DerivedData/c++_code-bbdyozxqxxdxosbxuyhcrqobxrkd/Build/Products/Debug/c++
code
Reason: image not found
This is the code I used i couldn't get int main to be inside the code block for some reason but anyway and I got this code from https://wiki.libsdl.org/SDL_CreateWindow.
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: enter program loop (see SDL_PollEvent)
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
I figured out why this was happening I was meant to put the framework in /Library/Frameworks folder before using it in XCode because when you download SDL it gives you a read me file and the read me file says to put it in that folder.
I should try reading all the text in read me files next time I guess. But if I try running this in XCode it will crash for some reason. (Makes sense because it says dyld: Library not loaded and we just put it in /Library/Frameworks)
I'm new to C++ and i have been following a tutorial on how to make pong using C++ and SDL. I've been following the tutorial word by word but now I've hit a road block. When i try to run i get an error saying that my SDL functions are not being recognized.
The error i get is: fatal error C1083: Cannot open include file: 'SDLmain': No such file or directory
Here is my code:
#include "SDL.h"
#include"SDL_ttf.h"
SDL_Surface screen;
SDL_Event occur;
void loadGame()
{
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
}
int main (int argc, char* args[])
{
loadGame();
bool running = true;
while(running == true)
{
}
return 0;
}
You should check your SDL lib and include paths. Make sure you've set the correct directories for your IDE to find the sdlmain files it needs.
Under VC++ Directories, Include directories, you should have C:\sdl2.0\include (or wherever you put SDL and its include subfolder), along with whatever else your projects need.
If this is already set... which version of SDL are you using (1.x or 2.x)? Is your version of SDL_TTF, and any other SDL add-ons, the same version?
Make sure that "SDL_main.h" is in the same folder as "SDL.h".
From "SDL.h":
//[...]
/**
* \file SDL.h
*
* Main include header for the SDL library
*/
#ifndef _SDL_H
#define _SDL_H
#include "SDL_main.h"
#include "SDL_stdinc.h"
#include "SDL_assert.h"
//[...]
So I'm trying to display a simply image with the SDL library, but when I use the function SDL_BlitSurface() nothing happens, and all I get is a black screen. I should also note that I have the .bmp file, the source, and the executable file all in the same directory.
//SDL Header
#include "SDL/SDL.h"
int main(int argc, char* args[])
{
//Starts SDL
SDL_Init(SDL_INIT_EVERYTHING);
//SDL Surfaces are images that are going to be displayed.
SDL_Surface* Hello = NULL;
SDL_Surface* Screen = NULL;
//Sets the size of the window (Length, Height, Color(bits), Sets the Surface in Software Memory)
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
//Loads a .bmp image
Hello = SDL_LoadBMP("Hello.bmp");
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
SDL_Flip(Screen);
//Pause
SDL_Delay(2000);
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
//Quits SDL
SDL_Quit();
return 0;
}
LoadBMP() is crap. Install SDL_image library
sudo apt-get install SDL_image SDL_image_dev
(not sure about the names of the packages. Just use aptitude or synaptic or whatever to find them)
and include it with
#include "SDL_image.h"
You load your image then with
SDL_Surface* Hello = IMG_Load("Hello.bmp");
if (!Hello){
printf("Ooops, something went wrong: %s\n", IMG_GetError());
exit(0);
}
Important: Note that you should always do an error check and print out the error.
if (!Hello) is the same as if (Hello == NULL)
have you tried blitting any other types of images? when I first started SDL I remember having issues with .bmp files. Try a .jpg or .png and get back to me whether your code works or not.
I had similar "Problems"; maybe it's a pre-Version, or version incompatible to your graphic-driver; let's figure out. SWSurface and Flip; as I remember, the Flip-functionality only works with double-buffered HW_Surface.
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
Please try Update instead of Flip.
SDL_Update(surface);
And nexttime :)
Hello = SDL_LoadBMP("Hello.bmp");
if(Hello != NULL) {
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
...
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
}
because SDL_FreeSurface(NULL) will crash your programm.