Trying to make sense of sdl1.3 features so i can make simple

Your quit function does nothing. It only calls exit. It’s pointless.

I also don’t see you call SDL_Init(SDL_INIT_VIDEO) before attempting to use the video API. You also never call SDL_DestroyRenderer, SDL_DestroyWindow, and SDL_Quit – so SDL may not shutdown properly.

here’s a simple SDL application; all it does is render a sprite in the center of the screen (feel free to use it as a base if you want):

Code:

#include <SDL.h>
#include <SDL_video.h>

#include <stdio.h>
#include <stdlib.h>

// Get an error from an SDL function that returns an integer
int error = 0;
#define SDL_IERROR(code, fname, on_error)
error = (code);
if(error != 0){
printf(“SDL returned errorcode %d (%s) from function %s\n”, error, SDL_GetError(), fname);
on_error;
}
// Get an error from an SDL function that returns a pointer.
#define SDL_PERROR(result, fname, on_error)
if(result == NULL){
printf(“SDL returned NULL from function %s\n”, fname);
on_error;
}

SDL_Window* window;
SDL_Texture* sprite;

void freerenderer()
{
if(window) SDL_DestroyRenderer(window);
}

void freewindow()
{
if(window) SDL_DestroyWindow(window);
window = NULL;
}

void freesprite()
{
if(sprite) SDL_DestroyTexture(sprite);
sprite = NULL;
}

#ifdef __cplusplus
extern “C”
#endif //__cplusplus
int SDL_main(int argc, char* argv[])
{
// only initialize video subsystem.
SDL_IERROR(SDL_Init(SDL_INIT_VIDEO), “SDL_Init”, exit(-1))
atexit(SDL_Quit);

// create a centered 800x600 window with title "SDL 1.3 Test". Non-resizable, but movable and with border.
window = SDL_CreateWindow("SDL 1.3 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
SDL_PERROR(window, "SDL_CreateWindow", exit(-2))
atexit(freewindow);

// create a renderer. We aren't picky about which one at all.
SDL_IERROR(SDL_CreateRenderer(window, -1, 0), "SDL_CreateRenderer", exit(-3))
atexit(freerenderer);

// load a sprite from a bitmap
SDL_Surface* tsprite = SDL_LoadBMP("sprite.bmp"); // alt: IMG_Load("sprite.format");
SDL_PERROR(tsprite, "SDL_LoadBMP", exit(-4));

// Specifying 0 for pixel format tells SDL to choose the closest one to the surface's format.
sprite = SDL_CreateTextureFromSurface(0, tsprite);
SDL_FreeSurface(tsprite); // we can free the surface here
SDL_PERROR(sprite, "SDL_CreateTextureFromSurface", exit(-5))
atexit(freesprite);

// render the sprite centered in 32x32 box
SDL_Rect spriterect;
spriterect.x = 384;
spriterect.y = 284;
spriterect.w = 32;
spriterect.h = 32;

bool looprunning = true;
while(looprunning)
{
    SDL_Event event;
    while(looprunning && SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_WINDOWEVENT:
            {
                if(event.window.event == SDL_WINDOWEVENT_CLOSE)
                {
                    looprunning = false;
                }
                break;
            }
            case SDL_QUIT:
            {
                looprunning = false;
                break;
            }
        }
    }
    SDL_RenderClear();
    SDL_RenderCopy(sprite, NULL, &spriterect);
    SDL_RenderPresent();
}

exit(0);

return 0;

}------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

Your quit function does nothing. It only calls exit. It’s pointless.

I also don’t see you call SDL_Init(SDL_INIT_VIDEO) before attempting to use the video API. You also never call SDL_DestroyRenderer, SDL_DestroyWindow, and SDL_Quit – so SDL may not shutdown properly.

here’s a simple SDL application; all it does is render a sprite in the center of the screen (feel free to use it as a base if you want):

Code:

You’ve got a few problems here:

#include
#include

#include
#include

I think your include files were mistaken for HTML tags when you pasted that in.

? ? exit(0);

? ? return 0;

exit(0); and return 0; both do exactly the same thing since you’re in
main(). You never actually reach the return statement.

Also, since you’re using #ifdef __cplusplus, you obviously expect that
this might be compiled as C rather than C++, in which case all your
declarations need to be at the start of your function, not mixed in
with the rest of your code.

If you planed on compiling as C++ only instead, you’re probably better
off using a class instead of atexit().On 12 January 2011 09:14, Nathaniel J Fries wrote:

I think maybe I’ll work on an SDL 1.3 tutorial.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/