Automatically initialize the video system and create a renderer to simplify use.
authorSam Lantinga <slouken@libsdl.org>
Wed, 28 Oct 2009 06:04:07 +0000
changeset 341764a60c5d502e
parent 3416 3d50171ac879
child 3418 fba883490f72
Automatically initialize the video system and create a renderer to simplify use.
src/video/SDL_sysvideo.h
src/video/SDL_video.c
test/Makefile.in
test/testspriteminimal.c
     1.1 --- a/src/video/SDL_sysvideo.h	Wed Oct 28 04:33:24 2009 +0000
     1.2 +++ b/src/video/SDL_sysvideo.h	Wed Oct 28 06:04:07 2009 +0000
     1.3 @@ -396,6 +396,7 @@
     1.4  #endif
     1.5  
     1.6  #define SDL_CurrentDisplay	(_this->displays[_this->current_display])
     1.7 +#define SDL_CurrentRenderer	(SDL_CurrentDisplay.current_renderer)
     1.8  
     1.9  extern SDL_VideoDevice *SDL_GetVideoDevice();
    1.10  extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode);
     2.1 --- a/src/video/SDL_video.c	Wed Oct 28 04:33:24 2009 +0000
     2.2 +++ b/src/video/SDL_video.c	Wed Oct 28 06:04:07 2009 +0000
     2.3 @@ -754,8 +754,10 @@
     2.4      SDL_Window *windows;
     2.5  
     2.6      if (!_this) {
     2.7 -        SDL_UninitializedVideo();
     2.8 -        return 0;
     2.9 +        /* Initialize the video system if needed */
    2.10 +        if (SDL_VideoInit(NULL, 0) < 0) {
    2.11 +            return 0;
    2.12 +        }
    2.13      }
    2.14      if (flags & SDL_WINDOW_OPENGL) {
    2.15          if (!_this->GL_CreateContext) {
    2.16 @@ -926,15 +928,28 @@
    2.17          SDL_UninitializedVideo();
    2.18          return NULL;
    2.19      }
    2.20 -    for (i = 0; i < _this->num_displays; ++i) {
    2.21 -        SDL_VideoDisplay *display = &_this->displays[i];
    2.22 -        for (j = 0; j < display->num_windows; ++j) {
    2.23 -            SDL_Window *window = &display->windows[j];
    2.24 -            if (window->id == windowID) {
    2.25 +    if (windowID) {
    2.26 +        for (i = 0; i < _this->num_displays; ++i) {
    2.27 +            SDL_VideoDisplay *display = &_this->displays[i];
    2.28 +            for (j = 0; j < display->num_windows; ++j) {
    2.29 +                SDL_Window *window = &display->windows[j];
    2.30 +                if (window->id == windowID) {
    2.31 +                    return window;
    2.32 +                }
    2.33 +            }
    2.34 +        }
    2.35 +    } else {
    2.36 +        /* Just return the first active window */
    2.37 +        for (i = 0; i < _this->num_displays; ++i) {
    2.38 +            SDL_VideoDisplay *display = &_this->displays[i];
    2.39 +            for (j = 0; j < display->num_windows; ++j) {
    2.40 +                SDL_Window *window = &display->windows[j];
    2.41                  return window;
    2.42              }
    2.43          }
    2.44      }
    2.45 +    /* Couldn't find the window with the requested ID */
    2.46 +    SDL_SetError("Invalid window ID");
    2.47      return NULL;
    2.48  }
    2.49  
    2.50 @@ -951,6 +966,23 @@
    2.51      return &_this->displays[window->display];
    2.52  }
    2.53  
    2.54 +static __inline__ SDL_Renderer *
    2.55 +SDL_GetCurrentRenderer()
    2.56 +{
    2.57 +    SDL_Renderer *renderer;
    2.58 +
    2.59 +    if (!_this) {
    2.60 +        SDL_UninitializedVideo();
    2.61 +        return NULL;
    2.62 +    }
    2.63 +    if (!SDL_CurrentRenderer) {
    2.64 +        if (SDL_CreateRenderer(0, -1, 0) < 0) {
    2.65 +            return NULL;
    2.66 +        }
    2.67 +    }
    2.68 +    return SDL_CurrentRenderer;
    2.69 +}
    2.70 +
    2.71  Uint32
    2.72  SDL_GetWindowFlags(SDL_WindowID windowID)
    2.73  {
    2.74 @@ -1506,31 +1538,29 @@
    2.75          return -1;
    2.76      }
    2.77      renderer = window->renderer;
    2.78 -    if (!renderer) {
    2.79 -        SDL_SetError("Renderer hasn't been created yet");
    2.80 -        return -1;
    2.81 -    }
    2.82 -    if (renderer->ActivateRenderer) {
    2.83 -        if (renderer->ActivateRenderer(renderer) < 0) {
    2.84 +    if (renderer) {
    2.85 +        if (renderer->ActivateRenderer) {
    2.86 +            if (renderer->ActivateRenderer(renderer) < 0) {
    2.87 +                return -1;
    2.88 +            }
    2.89 +        }
    2.90 +        SDL_CurrentDisplay.current_renderer = renderer;
    2.91 +    } else {
    2.92 +        if (SDL_CreateRenderer(windowID, -1, 0) < 0) {
    2.93              return -1;
    2.94          }
    2.95      }
    2.96 -    SDL_CurrentDisplay.current_renderer = renderer;
    2.97      return 0;
    2.98  }
    2.99  
   2.100  int
   2.101  SDL_GetRendererInfo(SDL_RendererInfo * info)
   2.102  {
   2.103 -    if (!_this) {
   2.104 -        SDL_UninitializedVideo();
   2.105 +    SDL_Renderer *renderer = SDL_GetCurrentRenderer();
   2.106 +    if (!renderer) {
   2.107          return -1;
   2.108      }
   2.109 -    if (!SDL_CurrentDisplay.current_renderer) {
   2.110 -        SDL_SetError("There is no current renderer");
   2.111 -        return -1;
   2.112 -    }
   2.113 -    *info = SDL_CurrentDisplay.current_renderer->info;
   2.114 +    *info = renderer->info;
   2.115      return 0;
   2.116  }
   2.117  
   2.118 @@ -1541,11 +1571,7 @@
   2.119      SDL_Renderer *renderer;
   2.120      SDL_Texture *texture;
   2.121  
   2.122 -    if (!_this) {
   2.123 -        SDL_UninitializedVideo();
   2.124 -        return 0;
   2.125 -    }
   2.126 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.127 +    renderer = SDL_GetCurrentRenderer();
   2.128      if (!renderer) {
   2.129          return 0;
   2.130      }
   2.131 @@ -1599,9 +1625,8 @@
   2.132      }
   2.133      fmt = surface->format;
   2.134  
   2.135 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.136 +    renderer = SDL_GetCurrentRenderer();
   2.137      if (!renderer) {
   2.138 -        SDL_SetError("No current renderer available");
   2.139          return 0;
   2.140      }
   2.141  
   2.142 @@ -2234,11 +2259,7 @@
   2.143  {
   2.144      SDL_Renderer *renderer;
   2.145  
   2.146 -    if (!_this) {
   2.147 -        SDL_UninitializedVideo();
   2.148 -        return -1;
   2.149 -    }
   2.150 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.151 +    renderer = SDL_GetCurrentRenderer();
   2.152      if (!renderer) {
   2.153          return -1;
   2.154      }
   2.155 @@ -2258,11 +2279,7 @@
   2.156  {
   2.157      SDL_Renderer *renderer;
   2.158  
   2.159 -    if (!_this) {
   2.160 -        SDL_UninitializedVideo();
   2.161 -        return -1;
   2.162 -    }
   2.163 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.164 +    renderer = SDL_GetCurrentRenderer();
   2.165      if (!renderer) {
   2.166          return -1;
   2.167      }
   2.168 @@ -2286,11 +2303,7 @@
   2.169  {
   2.170      SDL_Renderer *renderer;
   2.171  
   2.172 -    if (!_this) {
   2.173 -        SDL_UninitializedVideo();
   2.174 -        return -1;
   2.175 -    }
   2.176 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.177 +    renderer = SDL_GetCurrentRenderer();
   2.178      if (!renderer) {
   2.179          return -1;
   2.180      }
   2.181 @@ -2307,11 +2320,7 @@
   2.182  {
   2.183      SDL_Renderer *renderer;
   2.184  
   2.185 -    if (!_this) {
   2.186 -        SDL_UninitializedVideo();
   2.187 -        return -1;
   2.188 -    }
   2.189 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.190 +    renderer = SDL_GetCurrentRenderer();
   2.191      if (!renderer) {
   2.192          return -1;
   2.193      }
   2.194 @@ -2325,11 +2334,7 @@
   2.195      SDL_Renderer *renderer;
   2.196      SDL_Window *window;
   2.197  
   2.198 -    if (!_this) {
   2.199 -        SDL_UninitializedVideo();
   2.200 -        return -1;
   2.201 -    }
   2.202 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.203 +    renderer = SDL_GetCurrentRenderer();
   2.204      if (!renderer) {
   2.205          return -1;
   2.206      }
   2.207 @@ -2355,11 +2360,7 @@
   2.208          return SDL_RenderPoint(x1, y1);
   2.209      }
   2.210  
   2.211 -    if (!_this) {
   2.212 -        SDL_UninitializedVideo();
   2.213 -        return -1;
   2.214 -    }
   2.215 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.216 +    renderer = SDL_GetCurrentRenderer();
   2.217      if (!renderer) {
   2.218          return -1;
   2.219      }
   2.220 @@ -2386,11 +2387,7 @@
   2.221      SDL_Window *window;
   2.222      SDL_Rect real_rect;
   2.223  
   2.224 -    if (!_this) {
   2.225 -        SDL_UninitializedVideo();
   2.226 -        return -1;
   2.227 -    }
   2.228 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.229 +    renderer = SDL_GetCurrentRenderer();
   2.230      if (!renderer) {
   2.231          return -1;
   2.232      }
   2.233 @@ -2422,9 +2419,8 @@
   2.234      SDL_Rect real_srcrect;
   2.235      SDL_Rect real_dstrect;
   2.236  
   2.237 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.238 +    renderer = SDL_GetCurrentRenderer();
   2.239      if (!renderer) {
   2.240 -        SDL_SetError("No current renderer available");
   2.241          return -1;
   2.242      }
   2.243      if (!texture) {
   2.244 @@ -2483,11 +2479,7 @@
   2.245  {
   2.246      SDL_Renderer *renderer;
   2.247  
   2.248 -    if (!_this) {
   2.249 -        SDL_UninitializedVideo();
   2.250 -        return;
   2.251 -    }
   2.252 -    renderer = SDL_CurrentDisplay.current_renderer;
   2.253 +    renderer = SDL_GetCurrentRenderer();
   2.254      if (!renderer || !renderer->RenderPresent) {
   2.255          return;
   2.256      }
     3.1 --- a/test/Makefile.in	Wed Oct 28 04:33:24 2009 +0000
     3.2 +++ b/test/Makefile.in	Wed Oct 28 06:04:07 2009 +0000
     3.3 @@ -7,7 +7,7 @@
     3.4  CFLAGS  = @CFLAGS@
     3.5  LIBS	= @LIBS@
     3.6  
     3.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testpower$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) testatomic$(EXE) testime$(EXE)
     3.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmmousetablet$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     3.9  
    3.10  all: Makefile $(TARGETS)
    3.11  
    3.12 @@ -107,6 +107,9 @@
    3.13  testsem$(EXE): $(srcdir)/testsem.c
    3.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    3.15  
    3.16 +testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
    3.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    3.18 +
    3.19  testsprite$(EXE): $(srcdir)/testsprite.c
    3.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    3.21  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/testspriteminimal.c	Wed Oct 28 06:04:07 2009 +0000
     4.3 @@ -0,0 +1,165 @@
     4.4 +/* Simple program:  Move N sprites around on the screen as fast as possible */
     4.5 +
     4.6 +#include <stdlib.h>
     4.7 +#include <stdio.h>
     4.8 +#include <time.h>
     4.9 +
    4.10 +#include "SDL_events.h"
    4.11 +#include "SDL_video.h"
    4.12 +
    4.13 +#define WINDOW_WIDTH    640
    4.14 +#define WINDOW_HEIGHT   480
    4.15 +#define NUM_SPRITES     100
    4.16 +#define MAX_SPEED       1
    4.17 +
    4.18 +static SDL_TextureID sprite;
    4.19 +static SDL_Rect positions[NUM_SPRITES];
    4.20 +static SDL_Rect velocities[NUM_SPRITES];
    4.21 +static int sprite_w, sprite_h;
    4.22 +
    4.23 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    4.24 +static void
    4.25 +quit(int rc)
    4.26 +{
    4.27 +    exit(rc);
    4.28 +}
    4.29 +
    4.30 +int
    4.31 +LoadSprite(char *file)
    4.32 +{
    4.33 +    SDL_Surface *temp;
    4.34 +
    4.35 +    /* Load the sprite image */
    4.36 +    temp = SDL_LoadBMP(file);
    4.37 +    if (temp == NULL) {
    4.38 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    4.39 +        return (-1);
    4.40 +    }
    4.41 +    sprite_w = temp->w;
    4.42 +    sprite_h = temp->h;
    4.43 +
    4.44 +    /* Set transparent pixel as the pixel at (0,0) */
    4.45 +    if (temp->format->palette) {
    4.46 +        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    4.47 +    } else {
    4.48 +        switch (temp->format->BitsPerPixel) {
    4.49 +        case 15:
    4.50 +            SDL_SetColorKey(temp, SDL_TRUE,
    4.51 +                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
    4.52 +            break;
    4.53 +        case 16:
    4.54 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    4.55 +            break;
    4.56 +        case 24:
    4.57 +            SDL_SetColorKey(temp, SDL_TRUE,
    4.58 +                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    4.59 +            break;
    4.60 +        case 32:
    4.61 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    4.62 +            break;
    4.63 +        }
    4.64 +    }
    4.65 +
    4.66 +    /* Create textures from the image */
    4.67 +    sprite = SDL_CreateTextureFromSurface(0, temp);
    4.68 +    if (!sprite) {
    4.69 +        SDL_SetColorKey(temp, 0, 0);
    4.70 +        sprite = SDL_CreateTextureFromSurface(0, temp);
    4.71 +    }
    4.72 +    if (!sprite) {
    4.73 +        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    4.74 +        SDL_FreeSurface(temp);
    4.75 +        return (-1);
    4.76 +    }
    4.77 +    SDL_FreeSurface(temp);
    4.78 +
    4.79 +    /* We're ready to roll. :) */
    4.80 +    return (0);
    4.81 +}
    4.82 +
    4.83 +void
    4.84 +MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
    4.85 +{
    4.86 +    int i;
    4.87 +    int window_w = WINDOW_WIDTH;
    4.88 +    int window_h = WINDOW_HEIGHT;
    4.89 +    SDL_Rect *position, *velocity;
    4.90 +
    4.91 +    /* Draw a gray background */
    4.92 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    4.93 +    SDL_RenderFill(NULL);
    4.94 +
    4.95 +    /* Move the sprite, bounce at the wall, and draw */
    4.96 +    for (i = 0; i < NUM_SPRITES; ++i) {
    4.97 +        position = &positions[i];
    4.98 +        velocity = &velocities[i];
    4.99 +        position->x += velocity->x;
   4.100 +        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   4.101 +            velocity->x = -velocity->x;
   4.102 +            position->x += velocity->x;
   4.103 +        }
   4.104 +        position->y += velocity->y;
   4.105 +        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   4.106 +            velocity->y = -velocity->y;
   4.107 +            position->y += velocity->y;
   4.108 +        }
   4.109 +
   4.110 +        /* Blit the sprite onto the screen */
   4.111 +        SDL_RenderCopy(sprite, NULL, position);
   4.112 +    }
   4.113 +
   4.114 +    /* Update the screen! */
   4.115 +    SDL_RenderPresent();
   4.116 +}
   4.117 +
   4.118 +int
   4.119 +main(int argc, char *argv[])
   4.120 +{
   4.121 +    SDL_WindowID window;
   4.122 +    int i, done;
   4.123 +    SDL_Event event;
   4.124 +
   4.125 +    window = SDL_CreateWindow("Happy Smileys",
   4.126 +                              SDL_WINDOWPOS_UNDEFINED,
   4.127 +                              SDL_WINDOWPOS_UNDEFINED,
   4.128 +                              WINDOW_WIDTH, WINDOW_HEIGHT,
   4.129 +                              SDL_WINDOW_SHOWN);
   4.130 +    if (!window) {
   4.131 +        quit(2);
   4.132 +    }
   4.133 +
   4.134 +    if (LoadSprite("icon.bmp") < 0) {
   4.135 +        quit(2);
   4.136 +    }
   4.137 +
   4.138 +    /* Initialize the sprite positions */
   4.139 +    srand(time(NULL));
   4.140 +    for (i = 0; i < NUM_SPRITES; ++i) {
   4.141 +        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   4.142 +        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   4.143 +        positions[i].w = sprite_w;
   4.144 +        positions[i].h = sprite_h;
   4.145 +        velocities[i].x = 0;
   4.146 +        velocities[i].y = 0;
   4.147 +        while (!velocities[i].x && !velocities[i].y) {
   4.148 +            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   4.149 +            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   4.150 +        }
   4.151 +    }
   4.152 +
   4.153 +    /* Main render loop */
   4.154 +    done = 0;
   4.155 +    while (!done) {
   4.156 +        /* Check for events */
   4.157 +        while (SDL_PollEvent(&event)) {
   4.158 +            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   4.159 +                done = 1;
   4.160 +            }
   4.161 +        }
   4.162 +        MoveSprites(window, sprite);
   4.163 +    }
   4.164 +
   4.165 +    quit(0);
   4.166 +}
   4.167 +
   4.168 +/* vi: set ts=4 sw=4 expandtab: */