|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
SDL 1.3 video exampleAttached is a simple example using the new video API for use with the
latest SDL snapshot: http://www.libsdl.org/tmp/SDL-1.3.zip You'll notice a lot of the new complexity is automatically taken care of for you, if you don't want it. :) See ya! -- -Sam Lantinga, Founder and President, Galaxy Gameworks LLC [testspriteminimal.c] /* Simple program: Move N sprites around on the screen as fast as possible */ #include <stdlib.h> #include <stdio.h> #include <time.h> #include "SDL_events.h" #include "SDL_video.h" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 #define NUM_SPRITES 100 #define MAX_SPEED 1 static SDL_TextureID sprite; static SDL_Rect positions[NUM_SPRITES]; static SDL_Rect velocities[NUM_SPRITES]; static int sprite_w, sprite_h; /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ static void quit(int rc) { exit(rc); } int LoadSprite(char *file) { SDL_Surface *temp; /* Load the sprite image */ temp = SDL_LoadBMP(file); if (temp == NULL) { fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); return (-1); } sprite_w = temp->w; sprite_h = temp->h; /* Set transparent pixel as the pixel at (0,0) */ if (temp->format->palette) { SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels); } else { switch (temp->format->BitsPerPixel) { case 15: SDL_SetColorKey(temp, SDL_TRUE, (*(Uint16 *) temp->pixels) & 0x00007FFF); break; case 16: SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels); break; case 24: SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF); break; case 32: SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels); break; } } /* Create textures from the image */ sprite = SDL_CreateTextureFromSurface(0, temp); if (!sprite) { SDL_SetColorKey(temp, 0, 0); sprite = SDL_CreateTextureFromSurface(0, temp); } if (!sprite) { fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); SDL_FreeSurface(temp); return (-1); } SDL_FreeSurface(temp); /* We're ready to roll. :) */ return (0); } void MoveSprites(SDL_WindowID window, SDL_TextureID sprite) { int i; int window_w = WINDOW_WIDTH; int window_h = WINDOW_HEIGHT; SDL_Rect *position, *velocity; /* Draw a gray background */ SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderFill(NULL); /* Move the sprite, bounce at the wall, and draw */ for (i = 0; i < NUM_SPRITES; ++i) { position = &positions[i]; velocity = &velocities[i]; position->x += velocity->x; if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { velocity->x = -velocity->x; position->x += velocity->x; } position->y += velocity->y; if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { velocity->y = -velocity->y; position->y += velocity->y; } /* Blit the sprite onto the screen */ SDL_RenderCopy(sprite, NULL, position); } /* Update the screen! */ SDL_RenderPresent(); } int main(int argc, char *argv[]) { SDL_WindowID window; int i, done; SDL_Event event; window = SDL_CreateWindow("Happy Smileys", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); if (!window) { quit(2); } if (LoadSprite("icon.bmp") < 0) { quit(2); } /* Initialize the sprite positions */ srand(time(NULL)); for (i = 0; i < NUM_SPRITES; ++i) { positions[i].x = rand() % (WINDOW_WIDTH - sprite_w); positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h); positions[i].w = sprite_w; positions[i].h = sprite_h; velocities[i].x = 0; velocities[i].y = 0; while (!velocities[i].x && !velocities[i].y) { velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; } } /* Main render loop */ done = 0; while (!done) { /* Check for events */ while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) { done = 1; } } MoveSprites(window, sprite); } quit(0); } /* vi: set ts=4 sw=4 expandtab: */ _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleYou forgot to call SDL_quit in your quit function:
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ static void quit(int rc) { exit(rc); } 2009/10/28 Sam Lantinga <slouken@...>: > Attached is a simple example using the new video API for use with the > latest SDL snapshot: > http://www.libsdl.org/tmp/SDL-1.3.zip > > You'll notice a lot of the new complexity is automatically taken care > of for you, if you don't want it. :) > > See ya! > -- > -Sam Lantinga, Founder and President, Galaxy Gameworks LLC > > _______________________________________________ > SDL mailing list > SDL@... > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org > > SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video example2009/10/28 Kenneth Bull <llubnek@...>:
> You forgot to call SDL_quit in your quit function: > > /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ > static void > quit(int rc) > { > exit(rc); > } nvm, there's no SDL_Init() either. _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleYeah, there's also a memory leak because I don't free the window. I'm
still debating whether I want to enforce calling SDL_Quit() or have a reference counted de-init on the video system. On Wed, Oct 28, 2009 at 12:57 AM, Kenneth Bull <llubnek@...> wrote: > 2009/10/28 Kenneth Bull <llubnek@...>: >> You forgot to call SDL_quit in your quit function: >> >> /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ >> static void >> quit(int rc) >> { >> exit(rc); >> } > > nvm, there's no SDL_Init() either. > _______________________________________________ > SDL mailing list > SDL@... > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org > -- -Sam Lantinga, Founder and President, Galaxy Gameworks LLC _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleOn Wed, Oct 28, 2009 at 8:17 AM, Sam Lantinga <slouken@...> wrote:
> Yeah, there's also a memory leak because I don't free the window. I'm > still debating whether I want to enforce calling SDL_Quit() or have a > reference counted de-init on the video system. Both are fine with me. OTOH, I might want to completely shut down SDL and start it clean and fresh and then I would be wondering why I can't find SDL_Quit. I'd provide and SDL_Quit() even if it didn't do anything just to keep the 1,000,001 questions that all start "I can't find anything like SDL_Quit()...." off the list. Bob Pendleton > > On Wed, Oct 28, 2009 at 12:57 AM, Kenneth Bull <llubnek@...> wrote: >> 2009/10/28 Kenneth Bull <llubnek@...>: >>> You forgot to call SDL_quit in your quit function: >>> >>> /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ >>> static void >>> quit(int rc) >>> { >>> exit(rc); >>> } >> >> nvm, there's no SDL_Init() either. >> _______________________________________________ >> SDL mailing list >> SDL@... >> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org >> > > > > -- > -Sam Lantinga, Founder and President, Galaxy Gameworks LLC > _______________________________________________ > SDL mailing list > SDL@... > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org > -- +----------------------------------------------------------- + Bob Pendleton: writer and programmer + email: Bob@... + web: www.TheGrumpyProgrammer.com _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video example>----- Original Message ----
>From: Bob Pendleton <bob@...> >Subject: Re: [SDL] SDL 1.3 video example > >On Wed, Oct 28, 2009 at 8:17 AM, Sam Lantinga <slouken@...> wrote: >> Yeah, there's also a memory leak because I don't free the window. I'm >> still debating whether I want to enforce calling SDL_Quit() or have a >> reference counted de-init on the video system. > >Both are fine with me. OTOH, I might want to completely shut down SDL >and start it clean and fresh and then I would be wondering why I can't >find SDL_Quit. I'd provide and SDL_Quit() even if it didn't do >anything just to keep the 1,000,001 questions that all start "I can't >find anything like SDL_Quit()...." off the list. > >Bob Pendleton Agreed, but I'd take it one step further. Have SDL_Quit be part of the reference counting system. So for example, if I call SDL_Init(SDL_INIT_VIDEO) twice, it shouldn't shut down the video subsystem until the second SDL_Quit call. _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleOn Wed, Oct 28, 2009 at 12:37 PM, Mason Wheeler <masonwheeler@...> wrote:
>>----- Original Message ---- > >>From: Bob Pendleton <bob@...> >>Subject: Re: [SDL] SDL 1.3 video example >> >>On Wed, Oct 28, 2009 at 8:17 AM, Sam Lantinga <slouken@...> wrote: >>> Yeah, there's also a memory leak because I don't free the window. I'm >>> still debating whether I want to enforce calling SDL_Quit() or have a >>> reference counted de-init on the video system. >> >>Both are fine with me. OTOH, I might want to completely shut down SDL >>and start it clean and fresh and then I would be wondering why I can't >>find SDL_Quit. I'd provide and SDL_Quit() even if it didn't do >>anything just to keep the 1,000,001 questions that all start "I can't >>find anything like SDL_Quit()...." off the list. >> >>Bob Pendleton > > Agreed, but I'd take it one step further. Have SDL_Quit be part of the > reference counting system. So for example, if I call SDL_Init(SDL_INIT_VIDEO) > twice, it shouldn't shut down the video subsystem until the second > SDL_Quit call. Yeah, I can see that. If I call a library that uses SDL to pop up an interface that then goes away, having the library and my code both be able to init SDL and Quit SDL could be nice. But, that means they have to not share any values between the two instances of SDL. Otherwise if one sets something that the other has also set, you get inconsistent behavior. It doesn't seem like a good idea to me. Bob Pendleton > > _______________________________________________ > SDL mailing list > SDL@... > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org > -- +----------------------------------------------------------- + Bob Pendleton: writer and programmer + email: Bob@... + web: www.TheGrumpyProgrammer.com _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video example>----- Original Message ----
>From: Bob Pendleton <bob@...> >Subject: Re: [SDL] SDL 1.3 video example > >But, that means they have to not share any values between the two >instances of SDL. Otherwise if one sets something that the other >has also set, you get inconsistent behavior. It doesn't seem like a >good idea to me. OK, I'm lost. What sort of potential problems are you talking about here? _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video example2009/10/28 Bob Pendleton <bob@...>:
> Yeah, I can see that. If I call a library that uses SDL to pop up an > interface that then goes away, having the library and my code both be > able to init SDL and Quit SDL could be nice. But, that means they have > to not share any values between the two instances of SDL. Otherwise if > one sets something that the other has also set, you get inconsistent > behavior. It doesn't seem like a good idea to me. I did something like that for an instance class a while back... Not really useful though without actually putting it in SDL. See attached if you're interested. Wouldn't take much to move this to SDL_Init, SDL_Quit, etc. _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleHello !
> find SDL_Quit. I'd provide and SDL_Quit() even if it didn't do > anything just to keep the 1,000,001 questions that all start "I can't > find anything like SDL_Quit()...." off the list. It is natural, i would wonder too if there is a XYXY_Init, why is there no XYXY_Quit :-) CU _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video exampleOn Wed, Oct 28, 2009 at 1:33 PM, Mason Wheeler <masonwheeler@...> wrote:
>>----- Original Message ---- > >>From: Bob Pendleton <bob@...> >>Subject: Re: [SDL] SDL 1.3 video example >> >>But, that means they have to not share any values between the two >>instances of SDL. Otherwise if one sets something that the other >>has also set, you get inconsistent behavior. It doesn't seem like a >>good idea to me. > > OK, I'm lost. What sort of potential problems are you talking about > here? If SDL has any global variables or any internal state that my program can set. Then my program will expect it to stay the way I set it. If I use a library that does SDL_init() it will expect to be able to set the same internal state and expect it to stay the way it set it. So, if you want different parts of process to be able to init and quit SDL then SDL has to be written so that every SDL_init creates a separate instance of all the of SDL's internal state. The most obvious example is the event queue. SDL puts every event on one queue. If I want to do my own SDL_init then I also want to read events from the queue, right? How else would I be able to interact with the user? So, SDL would need a way to some how support two event queues. I am not talking about a second thread here, I am talking about a library. So, no, SDL should not allow multiple calls to SDL_init. Bob Pendleton > > _______________________________________________ > SDL mailing list > SDL@... > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org > -- +----------------------------------------------------------- + Bob Pendleton: writer and programmer + email: Bob@... + web: www.TheGrumpyProgrammer.com _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|
|
Re: SDL 1.3 video example2009/10/30 Bob Pendleton <bob@...>:
> If SDL has any global variables or any internal state that my program > can set. Then my program will expect it to stay the way I set it. If I > use a library that does SDL_init() it will expect to be able to set > the same internal state and expect it to stay the way it set it. So, > if you want different parts of process to be able to init and quit SDL > then SDL has to be written so that every SDL_init creates a separate > instance of all the of SDL's internal state. That's not all bad really... Create an SDL instance handle that can be passed in to various SDL_Create* functions. Store the instance handle in SDL_Surface, SDL_Window, etc. It does mean checking or switching instances on most API calls though. But this is back to global state vs passed state. Right now SDL is mostly global state. > The most obvious example is the event queue. SDL puts every event on > one queue. If I want to do my own SDL_init then I also want to read > events from the queue, right? How else would I be able to interact > with the user? So, SDL would need a way to some how support two event > queues. I am not talking about a second thread here, I am talking > about a library. And again, you could create a queue object to store events, then pass a queue handle to SDL_CreateWindow, SDL_PeepEvents, SDL_PushEvent, etc. Which actually could be a good thing, since you could get multiple threads, each with their own event queues, managing their own windows, even on Win32. > So, no, SDL should not allow multiple calls to SDL_init. Or you could have some way of informing the calling program/library that SDL has already been initialized. Other than checking SDL_WasInit for specific subsystems, you could have SDL_Init return 1 (instead of 0 or -1) if SDL itself has already been initialised (and maintain a reference count for SDL_Quit). Then code like so: int main(int, char**) { if (SDL_Init(0) < 0) { printf("Error initialising SDL: %s\n", SDL_GetError()); return 1; } if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { if (SDL_InitSubsystem(SDL_INIT_VIDEO) != 0) { printf("Error initialising SDL video: %s\n", SDL_GetError()); SDL_Quit(); return 2; } } /* ... */ SDL_Quit(); return 0; } _______________________________________________ SDL mailing list SDL@... http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
| Free embeddable forum powered by Nabble | Forum Help |