C++ - SDL not working - c++

I've tried to follow this tutorial on the basics of displaying an image with SDL. But, when I run the program, it returns a blank screen. The image is in the correct directories, but it doesn't display in the program. Am I doing something wrong? I'd really like SDL to work.
EDIT
Here is my code:
#include <SDL/SDL.h>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Surface *hello;
SDL_Surface *screen;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_FreeSurface(hello);
SDL_Quit();
return 0;
}

Use SDL_GetError() to find out why SDL_LoadBMP() fails loading your bitmap.
Read this thread too

I thought I said I had fixed this months ago, seems as though I had not. I recompiled it again and it worked, very strange.

Instead of SDL_Flip(screen) try using SDL_UpdateRect(screen,0,0,0,0)

Make sure that hello.bmp is in your current directory, and that it's a readable and valid BMP file.

In the line: "hello = SDL_LoadBMP("hello.bmp");" , try "/hello.bmp" instead of "hello.bmp". I had the same problem but it seems that putting the slash ("/") before the file name helps in find and therefore, render the image, even if it isn't on the same directory as the program.
If you are on Windows the instead of "/hello.bmp", write "c://hello.bmp". The first one was for Mac. (:
EDIT: Forget it, I just realized it actually doesn't work, it worked for me since I had the file on my disk directory.

Related

SDL OpenGL segmentation fault when using SDL_CreateWindow

I've got a weird problem that's suddenly appeared across all projects I'm working on. I'm using C++, SDL2 and OpenGL, and one of the first things that happens in my int main is to create an SDL window with an OpenGL flag like below:
int main( int argc, char* args[] )
{
//Minor stuff here e.g. initialising SDL
mainwindow = SDL_CreateWindow("...", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
}
For some reason this has started to cause a segmentation fault. If I change the flag from SDL_WINDOW_OPENGL to anything else, it does create a window but obviously fails shortly afterwards given the lack of an OpenGL context to do anything with. I've gone as far as to strip out all code except for the SDL and OpenGL initialisation stuff, and it still fails with a segfault error.
This issue has started as of today across two projects that share the same basic int main structure. This leads me to believe it's not a code issue (largely because the code hasn't actually changed), but that something with my setup / environment has gone wrong. So far I've tried the following to no avail:
Redownloaded latest SDL library
Redownloaded latest GLEW library
Reinstalled Codeblocks
Any ideas for a) what might be causing this and b) where I should start looking to fix it?
Thanks
Nathan
And like so many other problems in life, the answer turned out to be drivers. A system-wide update of some kind interfered with the graphics' ability to render any kind of OpenGL. A direct download and install of the latest graphic drivers fixed it.

Draw Text to Screen

So I just want to point out that I am pretty new to code outside engines so this is somewhat new to me.
I am using SDL as a base for my game and would like to know a easy way to draw text, in the form of score/time, on the screen.
So far when searching I have not found anything that I've really understood or how to use. The thing I find most when searching with the tag SDL is SDL_ttf and I've tried to look into it but with no success.
So again, I am looking for an easy way to display text, string and float/int, in the form of score/time.
Library SDL alone do not have support for writing text to the screen. Your search leads to SDL_ttf, which is right library to use.
Example of usage (only extra code, supposing you already called SDL_Init, created SDL_Window and you have SDL_Renderer* renderer for that window.
const SDL_Rect* dstrect;
SDL_Color color;
TTF_Init();
TTF_Font* font = TTF_OpenFont("font.ttf" /*path*/, 15 /*size*/);
SDL_Surface* textSurface = TTF_RenderText_Blended(font, "Text to render", color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_RenderCopy(renderer, textTexture, NULL, dstrect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
TTF_Quit();
Look into docs for other TTF_RenderText_* methods and how they differ.
And since you are using C++ (both SDL and SDL_ttf is in C), you probably want to write some wrappers around TTF rendering.
you'll want the SDL ttf library - it handles truetype fonts relatively straightforwardly.
http://www.libsdl.org/projects/SDL_ttf/
and documentation
http://jcatki.no-ip.org:8080/SDL_ttf/
http://jcatki.no-ip.org:8080/SDL_ttf/SDL_ttf.html
see especially documentation for TTF_OpenFont, TTF_CloseFont (to open & close a font file)
and the rendering functions eg TTF_RenderText_Solid
In your position I'd probably write a couple of helper functions to handle drawing text that wraps these, passing on the string, location to draw etc. and call them whenever you need to print score/etc.

Why do allegro crash when blitting? What does these debug reports mean?

I was programming in c++ using the allegro library in code::blocks. I declared a bitmap, loaded the bitmap and blit the image to test if the bitmap is being drawn into the screen. I compiled this program and the compiler found no errors. When I run the program, the bitmap is not drawn to the screen and allegro crashed. I changed the color depth and made sure that I included things in the code that won't make allegro crash but still no avail. I then run the debug to see if there's something wrong with this program. I get this message:
sigsegv segmentation fault
#0 10004DDD colorconv_blit_24_to_15() (C:\Windows\SysWOW64\alleg42.dll:??)
#1 0028FE3C ?? () (??:??)
#2 1006C3E0 get_uformat() (C:\Windows\SysWOW64\alleg42.dll:??)
What does this mean and how can I fix it? Are there any solution to this?
Here's my code:
$
#include <allegro.h>
using namespace std;
int main()
{
allegro_init();
install_keyboard();
set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
BITMAP *link=NULL;
link=load_bitmap("cave.bmp",NULL);
blit(link,screen,0,0,0,0,25,177);
readkey();
destroy_bitmap(link);
allegro_exit();
return 0;
}
END_OF_MAIN()
SOLVED!!!
I figured out why allegro crashed. I used Gimp to edit my bitmap before loading it on allegro in code::blocks. For everyone loading their bitmap using the old allegro library, do not use Gimp. Use any other image editing software like paint.net.
UPDATE:
I experimented my bitmap with gimp and it turns out that when I export my image as bitmap, another pop out came showing me the compatibility option and the advanced option. For everyone that uses the allegro library, ignore the top part. You can use Gimp but when you get that pop out after you click "export," click on the plus sign on the left of the compatibility option and that option will drop down and click that check that says "do not write color space information" and then click export. That should work and load fine on allegro.

Allegro Crashes When Loading Bitmap

I've looked around and their are similar questions but the answers haven't fixed my problem.
So, the problem is - If i try to load/draw bmp's in allegro it crashes, they are in the same directory as my project, they are named correctly, i believe the problem lies within the bmp itself. If i use the bmp's from the tutorial im using they work fine, however if i try to create my own, allegro crashes on startup, is there some sort of preset bmp i can use to create my sprites? I've gone over my code multiple times but if i missed something please point it out :)
My Code:
#include <allegro.h>
int main(int argc, char *argv[])
{
// Startup Stuff
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
// Bitmap Stuff
BITMAP *pic = NULL;
pic = load_bitmap("enemy.bmp", NULL);
blit(pic, screen, 0,0,0,0,200,150);//Draw the whole bitmap to the screen at (0,0)
readkey(); // Wait for Key Press.
destroy_bitmap(pic);
return 0;
}
END_OF_MAIN();
Tutorial Im Reading
Thanks in advance
The problem was the color depth, the bmp's i made were 16 bit, changing them to 24 bit fixed the problem!

When loading a .BMP image using SDL on visual studios 2012, it only shows a frame

I'm trying to make a game on visual studio 2012. I'm using SDL and I've set everything up correctly due to this tutorial: http://sdltutorials.com/sdl-tutorial-basics
I did look for other solutions on google: (1) place the image files in the project file. And, (2) place the images with the .exe program. A window does show but its black with no images. Both of these solutions failed. I'm on the edge of giving up on using visual studio 2012 for anything now. As for the code, I got the sources at the link I posted above. Thanks :)
EDIT:
When I build the project, its a success.
Im also using SDL-devel-1.2.15/SDL version 1.2.15.
My os system is windows 8.1 if that helps.
Here's the code for a short version of this example:
#include "SDL.h"
int main( int argc, char* args[] )
{
//The images
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Set up screen
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
//Load image
hello = SDL_LoadBMP( "hello.bmp" );
//Apply image to screen
SDL_BlitSurface( hello, NULL, screen, NULL );
//Update Screen
SDL_Flip( screen );
//Pause
SDL_Delay( 2000 );
//Free the loaded image
SDL_FreeSurface( hello );
//Quit SDL
SDL_Quit();
return 0;
}
The image shows. It was the code example from the website I posted. Can anyone find out what's wrong with his code(not the code I posted here. The code examples I shared in the website)?
Can anyone find out what's wrong with his code(not the code I posted here.
The code in tutorial (Win32_Sources.zip) you linked never calls SDL_Flip So even if you DO draw something on screen, you'll never see it.
Another problem is that this dude hasn't read SDL documentation, and attempts to free framebuffer (allocated with SDL_SetVideoMode) with SDL_FreeSurface. According to documentation, you shouldn't do that, because framebuffer is released by SDL_Quit.
(opinion)In addition to that I think his coding style is rather very poor - the has that "let's make a class for everything"(i.e. he loves OOP too much) problem, that some newbies have. Making separate *.cpp for a single function doesn't look like a good idea to me (it is 10..15 lines of code, for ****'s sake), I don't quite agree with his choice of CodeBlocks for project's IDE/build system, the code also kinda reminds me of MFC or Microsoft coding examples (where classes are named CSomething, etc). Overall, the guy looks like another newbie me so I don't think learning from him is a good idea.(/opinion)
I'd recommend to read SDL documentation instead of following tutorials (like this one). SDL is rather simple by itself, and its documentation contains short/simple examples which are normally written in C, which means there will be no OOP madness as in this guy's example.
For a build system or IDE on windows platform I'd suggest to use Visual Studio express (instead of codeblocks), however you should also learn some cross-platform build tool like CMake or qMake (part of Qt 4 or Qt 5). Learning those tools will make your life easier should you ever decide to transition to linux or something similar.
For a realtively good/clean C++ coding style, you should take a look at Qt 4/5 source code, tutorials and examples. Qt will be also useful shall you ever need GUI toolkit. Also consider trying OpenGL. Pure SDL is fine if you want to do MS-Dos style demos or software renderer, but OpenGL can do certain things faster.
You might consider upgrading to SDL2 and using a different set of tutorials like these that don't look too bad: http://twinklebeardev.blogspot.com/p/sdl-20-tutorial-index.html
If not you can use the same GetCurrentDirectory code to figure out what directory it is expecting the file to be in.
Here's a simple SDL2 example that loads and displays a bitmap. You can put breakpoints in the error checking code so you can see what's wrong if it fails. Make sure you're building a console style program so the errors have somewhere to print.
#include <Windows.h>
#include <iostream>
#include <string>
#include "SDL.h"
#pragma comment(lib, "sdl2.lib")
#pragma comment(lib, "sdl2main.lib")
int main(int argc, char* args[])
{
const std::string bmpFilename("test.bmp");
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if(!window)
{
std::cerr << "Could not create window!\n";
return 0;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(!renderer)
{
std::cerr << "Could not create renderer!\n";
return 0;
}
SDL_Surface* bmp = SDL_LoadBMP(bmpFilename.c_str());
if(!bmp)
{
char workingDirectory[_MAX_PATH];
GetCurrentDirectory(sizeof(workingDirectory), workingDirectory);
std::string fullPath(std::string(workingDirectory) + "\\" + bmpFilename);
std::cerr << "Could not load " << fullPath << "!\n";
return 0;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
if(!texture)
{
std::cerr << "Could not create texture!\n";
return 0;
}
SDL_FreeSurface(bmp);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}