• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DashProject

Beginner SDL app just quits as soon as it starts

2 posts in this topic

[font=Verdana, sans-serif]I am a beginner-to-intermediate C++ programmer in the process of teaching himself SDL, using a series of decent tutorials by Lazy Foo[/font][font=Verdana, sans-serif]. I got through the first lesson just fine, but then when I got to lesson two, "Optimized Surface Loading and Blitting[/font][font=Verdana, sans-serif]," a small amount of the terminologies started to confuse me, as some of it was over my head.[/font]

[font=Verdana, sans-serif]The program is supposed to display a tiled background with a message in the center, delay for two seconds then quit. But when I went to run the program it compiled just fine, with no errors and no warnings, only it just exits as soon as it starts, and I don't see the images loaded or anything. I want to make it very clear that 'sdl.dll' [i]is[/i] in the program's directory, and so are the files 'img_bg.bmp' and 'img_message.bmp' so it's not a matter of missing files. [/font]

[font=Verdana, sans-serif]My only theory as to why it's not displaying the images and waiting for 2000 milliseconds is because it ran into an error and one of the [i]return[/i][/font][font=Verdana, sans-serif] statements (see my code below,) and as such returned a one to exit the program. I don't know what else it could be, and I would love some help debugging my program.[/font]

[font=Verdana, sans-serif][url="http://www.lazyfoo.net/SDL_tutorials/lesson02/index.php"]http://www.lazyfoo.n...son02/index.php[/url] - Here's the tutorial I was following[/font]

[font=Verdana, sans-serif]My code is below, but it's rather tl;dr at the moment, so if you think you can help me without looking at the code, be my guest. I had to include the whole file because I don't know what line is causing the crash[/font]

[font=Verdana, sans-serif][b]PROTIP:[/b] If you are going to be a sarcastic prick and tell me something like 'rtfm' or 'lrn 2 debug' then I will not appreciate it. Instead, say it nicely and I will gladly go out and research what I need to in order to solve my own problem, at is, if you are disgusted with a beginner who is utterly lost and does not know where to go next (even some pointers, not the programming kind, to what I can do next on my own would be most helpful) and prefer to take your disgust out on me, it will not be tolerated.[/font]


[CODE]
/*****************************************
* OPTIMIZED SURFACE LOADING AND BLITTING *
* by Nick Hagen *
* 6/3/2012 *
******************************************
*
* Followed Lazy Foo's SDL tutorial here:
* http://www.lazyfoo.net/SDL_tutorials/lesson02/index.php
*
* Made with SDL 1.2.15
* and Code::Blocks 10.05
*/


/// *** INIT SECTION ************************************
// Required headers

#include "SDL/SDL.h"
#include <string> /* 'string' included because Lazy Foo prefers
'string' to 'char' */
using namespace std;

// Screen attributes, as global constants

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32; /* In all of Lazy Foo's tutorials,
32 BPP will be used */

/* I can sorta see how putting these constants into functions
would be preferably to typing '640' and '480' and '32' over
and over again. */


// Now we're declaring all the surfaces we will be using
SDL_Surface *message = NULL; // Foreground image
SDL_Surface *background = NULL; // Background image
SDL_Surface *screen = NULL;

/* Wait...I thought declaring global variables was bad programming
practice! I guess not in this case... */

/// *** FUNCTION DEFS ***********************************
/* Apparently 'loadImage()' is already a taken function name. I will
have to settle for calling it 'loadImages()' instead. Also, notice
the asterisk. I think that means that the function 'loadImages()'
returns a pointer to a variable of type 'SDL_Surface', but I could
be wrong. Anyways onwards! */


/// -- loadImages() -------------------------------------

SDL_Surface *loadImages(string filename)
{ // The argument 'filename' will hold the path of the image to be loaded
SDL_Surface* loadedImage = NULL; // This pointer stores the first loaded image
SDL_Surface* optimizedImage = NULL; // This one is for the optimized image

// Load the image (not QUITE sure about the '.c_str()' at the end of 'filename'
loadedImage = SDL_LoadBMP(filename.c_str());
/* My guess is that the '.c_str()' appends a filetype which is a C-string to the
end of the 'filename' string variable, but I could be mistaken. */
// If nothing went wrong in loading the image

if(loadedImage != NULL) {
// Create an optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
/* What 'SDL_DisplayFormat()' does is convert whatever BPP the loaded image
was to the BPP of the screen, in this case 32 BPP. Otherwise if you don't
convert the image's BPP when you load them here, SDL tries to convert on-
the-fly, which costs you speed and processing power. This, it's a much
better idea to convert images when you first load them.THAT is what 'optimize'
means, by the way. */

SDL_FreeSurface(loadedImage); // Get rid of the old, non-32 BPP image from memory
} // End 'if' statement


return optimizedImage; // Return the new image, optimized at 32 BPP

} // End function definition

/// -- applySurface() -----------------------------------
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{ // Takes in the coordinates of where you want to blit the surface, what surface to blit, and what surface to blit to
SDL_Rect offset; // 'SDL_Rect' is either a struct or a class. This instance will hold the offsets
offset.x = x;
offset.y = y;


// Actually blit the surface
SDL_BlitSurface(source, NULL, destination, &offset);
/* The fourth argument holds the offsets to where on the destination the source is going to be applied. I'm not
sure why it has a '&' in front of it though... */
} // End function definition


/// *** MAIN FUNCTION ***********************************
int main(int argc, char* args[])
{
/// -- Initializations ------------------------------
/* Initialize all SDL subsystems (I guess putting it in an 'if' statement still works...)
NOTES: SDL subsystems are things like the video, audio, timers, etc that are the individual
engine components used to make a game. */

if( SDL_Init(SDL_INIT_EVERYTHING) == -1){
return 1; /* If SDL can't initialize, it returns -1. In this case we handle
the error by returning 1, which will end the program */
} // End 'if' statement


SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); // Set up the screen
if(screen == NULL){ return 1;} //If there was an error in setting up the screen, end the program
SDL_WM_SetCaption("Hello, world!", NULL); // Set the window caption

/// -- Load and Apply -------------------------------

message = loadImages("img_hello.bmp");
background = loadImages("img_bg.bmp");


// Apply the background images! Tile them!

applySurface(0, 0, background, screen);
applySurface(320, 0, background, screen);
applySurface(0, 240, background, screen);
applySurface(320, 240, background, screen);

// Apply the message image!
applySurface(180, 140, message, screen);


// Flip the buffers, and update the screen
if(SDL_Flip(screen) == -1){ return 1;} // If there's an error then end the program
SDL_Delay(2000); // Wait 2 seconds


/// -- Finishing Up ---------------------------------
SDL_FreeSurface(message);
SDL_FreeSurface(background);
SDL_Quit(); // Quit SDL
return 0; // End of the program
}
[/CODE] Edited by Da5ch
-1

Share this post


Link to post
Share on other sites
you could print some debug messages (just a simple cout <<"Failed to init SDL"; or cout <<"Failed to flip buffers";) before each of your return 1s to figure out which one it is hitting.

also, in your post you said you had the files img_bg.bml and img_message.bmp but in the code you got img_hello.bmp, did you just make a mistake writing the post or could this be the cause of the problem ?
0

Share this post


Link to post
Share on other sites
[quote]
I want to make it very clear that 'sdl.dll' is in the program's directory, and so are the files 'img_bg.bmp' and 'img_message.bmp' so it's not a matter of missing files.
[/quote]
This is an assumption. The file might appear to be present, but it could be corrupted. In addition, if you are using Windows then the file explorer will hide the file extensions of "known" files by default. You might call a file image.bmp, but there could be an implicit, invisible ".bmp" extension, making the full file name "image.bmp.bmp".

I don't think these are your actual problems, but I'm just pointing this out for future reference. You should put error checking code in for this anyway, for reasons similar to what I have to say next.

[quote]
My only theory as to why it's not displaying the images and waiting for 2000 milliseconds is because it ran into an error and one of the return statements (see my code below,) and as such returned a one to exit the program. I don't know what else it could be, and I would love some help debugging my program.
[/quote]
This is very likely. There are a couple of ways of trying to diagnose this, such as outputting a message to a persistent log just before returning, using an OS API call to generate a message box, or placing a breakpoint on each of the return statements and running the program in your debugger.

The latter will find it fastest, but you should implement one of the former too, because someday your code will be crashing on someone else's machine - one that you can't attach a debugger to. To make it easy for you to figure out what is happening in such cases, you should log or display what failed, and the error message (i.e. SDL_GetError()).

[quote]
PROTIP: If you are going to be a sarcastic prick and tell me something like 'rtfm' or 'lrn 2 debug' then I will not appreciate it. Instead, say it nicely and I will gladly go out and research what I need to in order to solve my own problem, at is, if you are disgusted with a beginner who is utterly lost and does not know where to go next (even some pointers, not the programming kind, to what I can do next on my own would be most helpful) and prefer to take your disgust out on me, it will not be tolerated.
[/quote]
There should be no need to place such a message in your posts. We strongly discourage such unhelpful responses in general, but in particular in [i]For Beginners[/i] we have a much stricter policy towards being helpful. If you feel someone has crossed the line, bring it to the attention of a moderator via the "report" button (we don't see everything).

Speaking of discouraging behaviour, drop that pre-emptively defensive attitude in future. It will put people off answering, and it starts the thread on an aggressive tone.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0