• 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

SDL_BlitSurface doing nothing

4 posts in this topic

[color=#000000][font=Arial,]I can't seem to draw to the screen at all. When I call SDL_BlitSurface, nothing happens. However, it does not return an error code. I've written a class to handle image sprites, and when I step through with a debugger, I see it is returning non-empty values for the surface to blit to the main video surface. When I call my draw function, however, it does nothing. I have confirmed that the pixel values are not changed at all for the surface. What am I doing wrong?[/font][/color]

[color=#000000][font=Arial,]Here's my main.cpp:[/font][/color]

[color=#000000][font=Arial,]//Standard libs
#include <SDL.h>

//Our gaming base
#include "game_base.h"

//SDL redefines main as winmain, but this is a console application so we don't want that
#undef main

const int MAX_FPS = 80; ///< The maximum frames per second for the game

int main ( int argc, char* argv[] ) {
System game( false, 640, 480, "Platformer", MAX_FPS ); //Create a game that is not fullscreen with a resolution of 640x480
SDL_Surface *buffer = NULL;
Input *input = NULL;
Image_Sprite *player_sprite = NULL;

buffer = game.get_buffer();
player_sprite = new Image_Sprite( "data/player.bmp", 1, 4 );

//Setup the hitbox for the player
SDL_Rect hitbox;
hitbox.w = 32;
hitbox.h = 32;
hitbox.x = 0;
hitbox.y = 0;

Player player( 100, 100, hitbox, 1.0, RIGHT, player_sprite ); //Initialize the player at (100, 100)

//Main game loop
while( !game.check_is_done() ) {
player.draw( buffer );

delete player_sprite;

[color=#000000][font=Arial,]My draw method looks like this:[/font][/color][/color]

[color=#000000][color=#000000][font=Arial,]void Character::draw( SDL_Surface *destination ) {
SDL_Rect coordinates;
SDL_Surface *sprite;
sprite = my_sprite->get_frame( my_frame, my_direction );
coordinates.x = (int)get_x();
coordinates.y = (int)get_y();

SDL_BlitSurface( sprite, NULL, destination, NULL );


[color=#000000][font=Arial,]And my get_frame method is this:[/font][/color][/color]

[color=#000000][color=#000000][font=Arial,]SDL_Surface* Image_Sprite::get_frame( int x, int y ) {
SDL_PixelFormat *format;
format = my_sprite->format;
SDL_Surface *frame = SDL_CreateRGBSurface( my_sprite->flags, my_frame_width, my_frame_height, format->BitsPerPixel,
format->Rmask, format->Gmask, format->Bmask, format->Amask );
SDL_Rect *frame_crop = new SDL_Rect;
frame_crop->x = my_frame_width * x;
frame_crop->y = my_frame_height * y;
frame_crop->w = my_frame_width;
frame_crop->h = my_frame_height;

SDL_Rect *coordinates = new SDL_Rect;
coordinates->x = 0;
coordinates->y = 0;

SDL_BlitSurface( my_sprite, frame_crop, frame, coordinates );

delete frame_crop;
return frame;


[color=#000000]The funny thing is, this code is almost directly copied from another project where it worked fine. And yes, I am calling SDL_Init(), SDL_Quit, and SDL_Flip(). They're all part of my System class. Any help would be appreciated. I tried solving this myself, but I'm stumped.[/color][/color]

Share this post

Link to post
Share on other sites
Alright, I figured it out. I wasn't initializing my_direction, so I was getting nonsense values for where to crop the image.

Share this post

Link to post
Share on other sites
Don't forget that you are also not initializing the w and h fields in the SDL_Rect named coordinates, so attempting to blit a square of unknown dimensions would invoke undefined behavior.

Share this post

Link to post
Share on other sites
SDL is documented as not reading the width and height of the rightmost rectangle pointer parameter. In fact, this function actually initialises these values with the clipped range to be blitted! The dimensions to blit are solely based on the source rectangle.

That said, the function would be better written as:

SDL_Surface* Image_Sprite::get_frame( int x, int y ) {
SDL_PixelFormat *format = my_sprite->format;
SDL_Surface *frame = SDL_CreateRGBSurface(
my_sprite->flags, my_frame_width, my_frame_height,
format->BitsPerPixel, format->Rmask, format->Gmask, format->Bmask, format->Amask );

SDL_Rect frame_crop = {
my_frame_width * x,
my_frame_height * y,

SDL_BlitSurface( my_sprite, &frame_crop, frame, NULL );

return frame;
This avoids the possibility of undefined behaviour, and also avoids a memory leak. Passing NULL as the destination defaults to the top left.

If the destination were not at the top left corner, writing the following avoids undefined behaviour:

SDL_Rect source = { /* ... */ };
SDL_Rect dest = { x, y };

SDL_BlitSurface( surface, &source, frame, &dest );
Here, the width and height members of the destination rectangle are zeroed (due to the default zero-initialisation of incomplete aggregate initialisers - I cannot recall the technical name for this).

Share this post

Link to post
Share on other sites
Gah, foiled. I must have mistook it for my own image handling; if the width and height don't match, the source rectangle is scaled to match the size of the destination rectangle. However, explicitly setting your members, be it by initialization lists or assignment operators, will ensure that if they do change it to something like setting it to zero will use the original dimensions while a non-zero value will scale to fit, the behavior does not change unexpectedly.

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