• 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
kaktusas2598

Converting SDL code from C++ to C

4 posts in this topic

Hello all I am refreshing my kwoledge on SDL, using Lazy foo tutorials, and I need to code all my projects for SDL in C. So I convert all his C++ code to C. Bet I have serious issue in 12th tutorial about timing. Here is my code:
[source lang="cpp"]#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface* message = NULL;
SDL_Surface* seconds = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;

TTF_Font* font = NULL;
SDL_Color textColor = {255,0,0};//white, same 0xff,0xff,0xff

SDL_Surface *load_image( char *filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if (optimizedImage != NULL)
{
//map the color key
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0,0xFF,0xFF);
//sets all pixels of color R0, G 0xFF, B 0xFF to be tranparent
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
}
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );
}

int init(char * caption)
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 0;
}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if( screen == NULL )
{
return 0;
}
//initiliaze SDL_ttf
if (TTF_Init() == -1)
{
return 0;
}
SDL_WM_SetCaption( caption, NULL );
return 1;
}
int load_files()
{
background = load_image("background.png");
//open the font
font = TTF_OpenFont("lazy.ttf",30);
if( background == NULL )
{
return 0;
}
if( font == NULL )
{
return 0;
}
return 1;
}
void clean_up()
{
SDL_FreeSurface( background );
SDL_FreeSurface( message );
TTF_CloseFont(font);

TTF_Quit();
SDL_Quit();
}

int main( int argc, char* args[] )
{
int quit = 0;//make sure program waits for quit

//the timer starting time
Uint32 start = 0;

//the timer start/stop flag
int running = 0;

if( init("Simple timer") == 0 )return 1;

if(load_files() == 0) return 1;

message = TTF_RenderText_Solid(font,"Press s to start/stop the timer", textColor);
if (message == 0) return 1;

//start the timer
start = SDL_GetTicks();

//main loop
while (!quit)//while user hasnt quit
{
//while there's event to handle!
while(SDL_PollEvent(&event))
{
//if user has Xed out
if(event.type == SDL_QUIT)
{
//quit the program
quit = 1;
}
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_s)
{
//if timer is running
if(running)
{
//stop the timer
running = 0;
start = 0;
}
else
{
//start the timer
running = 1;
start = SDL_GetTicks();
}
}
else if(event.key.keysym.sym == SDLK_ESCAPE)
{
quit = 1;
}
}
//rendering
apply_surface(0,0,background,screen);
apply_surface((SCREEN_WIDTH - message->w)/2,SCREEN_HEIGHT/2,message,screen);

//if the timer is running
if(running)
{
//timer: as a string
char* time = "Timer: ";
//current time as a string
char* current = 0;
//convert timers time to a string
itoa((SDL_GetTicks() - start), current, 10);
//adds time (Timer: ) + time in ms
strcat(time, current);
//renders the time surface
seconds = TTF_RenderText_Solid(font, time, textColor);
//apply the times surface
apply_surface((SCREEN_WIDTH - seconds->w)/2,50,seconds,screen);
SDL_FreeSurface(seconds);
}

//updating
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
}
clean_up();
return 0;
}
[/source]
It doesn't have any compile or link errors, I can launch it, surfaces loads, but when I press 's'(to start/stop timer) it suddenly quits. I don't have idea what the problem might be, but I think issue in my "C version" of converting SDL time to string (Lazy Foo used string streams to handle this). This is the code where I used some standart C functions for doing so:
[source lang="cpp"]//rendering
apply_surface(0,0,background,screen);
apply_surface((SCREEN_WIDTH - message->w)/2,SCREEN_HEIGHT/2,message,screen);

//if the timer is running
if(running)
{
//timer: as a string
char* time = "Timer: ";
//current time as a string
char* current = 0;
//convert timers time to a string
itoa((SDL_GetTicks() - start), current, 10);
//adds time (Timer: ) + time in ms
strcat(time, current);
//renders the time surface
seconds = TTF_RenderText_Solid(font, time, textColor);
//apply the times surface
apply_surface((SCREEN_WIDTH - seconds->w)/2,50,seconds,screen);
SDL_FreeSurface(seconds);
}[/source]
0

Share this post


Link to post
Share on other sites
[quote name='kaktusas2598' timestamp='1353284391' post='5002172']
[CODE]
//timer: as a string
char* time = "Timer: ";
/*...*/
strcat(time, current);
[/CODE]
[/quote]

The length of "time" is fixed at 7. You can't concatenate anything to it.
0

Share this post


Link to post
Share on other sites
Besides what ultramailman said...

[code]char* current = 0;
//convert timers time to a string
itoa((SDL_GetTicks() - start), current, 10);[/code]

I am not sure what the semantics of you itoa function are (itoa is not a standard function), but you typically have to pass a pointer to a buffer big enough to hold the answer. If you pass 0, you are asking for trouble.

You don't seem to have a grasp of how to handle strings in C. Try to read up on it, because it's not easy to teach you about it in a forum post.

Why don't you want to use C++ anyway? C++ provides much cleaner ways to handle this type of thing.
1

Share this post


Link to post
Share on other sites
Besides your C string issues, do you also realize your rendering code will only run while this is true: ?
[code]
while(SDL_PollEvent(&event))
[/code]
since all the rendering is done within that loop (meaning, it only renders when it gets an event, typically an input event).
1

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