• 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
Raman Gupta

SDL very basic code

3 posts in this topic

Hi! everybody.
The code below should create a window with image.bmp as its background image.But when I run it creates a window without the image.I tried everything,my image is in the same folder of my project and main.cpp. The code is exactly same from a youtube tutorial and works fine for many people.Please tell what's wrong in this.
Thanks in advance.

#include<SDL/SDL.h>

SDL_Surface *screen=NULL;
SDL_Surface *background=NULL;

bool gamerunning = true;
SDL_Event event;

void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination){
SDL_Rect rect;
rect.x=x;
rect.y=y;
SDL_BlitSurface(source,NULL,destination,&rect);
}
int main(int argc,char *argv[]){

SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Apply Images",NULL);

screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
background=SDL_LoadBMP("image.bmp");




//Game Loop
while(gamerunning){
if(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT)
gamerunning=false;
}
//Apply surface
apply_surface(0,0,background,screen);

//Update Screen
SDL_Flip(screen);
}




SDL_Quit();
return 0;
}
0

Share this post


Link to post
Share on other sites
There are a few things you can try. Easiest is to check if the image has been correctly loaded:
[code]
background = SDL_LoadBMP("image.bmp");
if(!background)
{
// Write the error somewhere.
// In C (requires <stdio.h>)
printf("Failed to load image.bmp: %s\n", SDL_GetError());
// In C++ (requires <iostream>)
std::cout << "Failed to load image.bmp: " << SDL_GetError() << '\n';
}
[/code]
By implementing this and looking at the output, you should know if the file loaded correctly. If not, consider hard coding a full path to the file (remember to correctly escape the path if using windows back slashes). Or download a bitmap file you know is correct. It is unlikely - but your file might be corrupt. It might be a 32 bit bitmap, which has patchy support. It might look like "image.bmp" in Windows Explorer, but unless you've set the option to always show file extensions, the file might actually be called image.bmp.bmp (or even image.bmp.png).

You should add such error checking to all SDL functions, including SDL_Init() and SDL_SetVideoMode(). Even SDL_BlitSurface() can return an error. Better to be aware of these errors when they occur ather than spending time chasing phantom bugs later in your program.

If it is loading correctly, but no displaying, you should note that your rendering logic is tied to your input loop. That is, at least one "event" must occur before the screen will be drawn. That said, I would expect a SDL_VIDEOEXPOSE event to be delivered to your application during the first loop, so this shouldn't be an issue.
0

Share this post


Link to post
Share on other sites
@rip off
I have put the error check you have given but still nothing is displayed.The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp .
I didn't understand the SDL_VIDEOEXPOSE event.
0

Share this post


Link to post
Share on other sites
It would help if you post your current code, so we can check if your new error checking/handling is correct.

[quote]
I have put the error check you have given but still nothing is displayed
[/quote]
Can you verify that the program definitely doesn't enter the error handling code? Rather than, say, the output merely going somewhere you aren't checking. There are a couple of ways of doing this.

[list]
[*] Step through the program in the debugger, and see whether the error branch is executed.
[*] Output a message in an "else" clause. If you see no output then maybe you have more work to do in your error handling.
[*] Throw in a platform specific notification, for example a MessageBox() call on Windows.
[/list]

[quote]
The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp
[/quote]
Should work. Another likely problem is that your programs "working directory" isn't where you expect it to be. You said that your image is in the same folder as your source code. Depending on which IDE / toolchain you are using, that will not be where relative paths will be resolved against.

[quote]
I didn't understand the SDL_VIDEOEXPOSE event.
[/quote]
The event indicates that the video surface has been "exposed". For instance, if the window was hidden and then the user clicks on the entry in the task bar to bring the window back. Or if another window went in front of your program's one. It generally doesn't matter for games, because they often re-draw the entire screen every game loop.

However, if you were making a turn based or other non-active simulation game (e.g. chess, minesweeper), you might only draw the window when something changes. In this case you need to be notified when the stuff you have drawn becomes invalidated due to external events.
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