diff src/video/SDL_video.c @ 3417:64a60c5d502e

Automatically initialize the video system and create a renderer to simplify use.
author Sam Lantinga <slouken@libsdl.org>
date Wed, 28 Oct 2009 06:04:07 +0000
parents b334b4f7dfa0
children 032e64bbb56c
line wrap: on
line diff
--- a/src/video/SDL_video.c	Wed Oct 28 04:33:24 2009 +0000
+++ b/src/video/SDL_video.c	Wed Oct 28 06:04:07 2009 +0000
@@ -754,8 +754,10 @@
     SDL_Window *windows;
 
     if (!_this) {
-        SDL_UninitializedVideo();
-        return 0;
+        /* Initialize the video system if needed */
+        if (SDL_VideoInit(NULL, 0) < 0) {
+            return 0;
+        }
     }
     if (flags & SDL_WINDOW_OPENGL) {
         if (!_this->GL_CreateContext) {
@@ -926,15 +928,28 @@
         SDL_UninitializedVideo();
         return NULL;
     }
-    for (i = 0; i < _this->num_displays; ++i) {
-        SDL_VideoDisplay *display = &_this->displays[i];
-        for (j = 0; j < display->num_windows; ++j) {
-            SDL_Window *window = &display->windows[j];
-            if (window->id == windowID) {
+    if (windowID) {
+        for (i = 0; i < _this->num_displays; ++i) {
+            SDL_VideoDisplay *display = &_this->displays[i];
+            for (j = 0; j < display->num_windows; ++j) {
+                SDL_Window *window = &display->windows[j];
+                if (window->id == windowID) {
+                    return window;
+                }
+            }
+        }
+    } else {
+        /* Just return the first active window */
+        for (i = 0; i < _this->num_displays; ++i) {
+            SDL_VideoDisplay *display = &_this->displays[i];
+            for (j = 0; j < display->num_windows; ++j) {
+                SDL_Window *window = &display->windows[j];
                 return window;
             }
         }
     }
+    /* Couldn't find the window with the requested ID */
+    SDL_SetError("Invalid window ID");
     return NULL;
 }
 
@@ -951,6 +966,23 @@
     return &_this->displays[window->display];
 }
 
+static __inline__ SDL_Renderer *
+SDL_GetCurrentRenderer()
+{
+    SDL_Renderer *renderer;
+
+    if (!_this) {
+        SDL_UninitializedVideo();
+        return NULL;
+    }
+    if (!SDL_CurrentRenderer) {
+        if (SDL_CreateRenderer(0, -1, 0) < 0) {
+            return NULL;
+        }
+    }
+    return SDL_CurrentRenderer;
+}
+
 Uint32
 SDL_GetWindowFlags(SDL_WindowID windowID)
 {
@@ -1506,31 +1538,29 @@
         return -1;
     }
     renderer = window->renderer;
-    if (!renderer) {
-        SDL_SetError("Renderer hasn't been created yet");
-        return -1;
-    }
-    if (renderer->ActivateRenderer) {
-        if (renderer->ActivateRenderer(renderer) < 0) {
+    if (renderer) {
+        if (renderer->ActivateRenderer) {
+            if (renderer->ActivateRenderer(renderer) < 0) {
+                return -1;
+            }
+        }
+        SDL_CurrentDisplay.current_renderer = renderer;
+    } else {
+        if (SDL_CreateRenderer(windowID, -1, 0) < 0) {
             return -1;
         }
     }
-    SDL_CurrentDisplay.current_renderer = renderer;
     return 0;
 }
 
 int
 SDL_GetRendererInfo(SDL_RendererInfo * info)
 {
-    if (!_this) {
-        SDL_UninitializedVideo();
+    SDL_Renderer *renderer = SDL_GetCurrentRenderer();
+    if (!renderer) {
         return -1;
     }
-    if (!SDL_CurrentDisplay.current_renderer) {
-        SDL_SetError("There is no current renderer");
-        return -1;
-    }
-    *info = SDL_CurrentDisplay.current_renderer->info;
+    *info = renderer->info;
     return 0;
 }
 
@@ -1541,11 +1571,7 @@
     SDL_Renderer *renderer;
     SDL_Texture *texture;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return 0;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return 0;
     }
@@ -1599,9 +1625,8 @@
     }
     fmt = surface->format;
 
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
-        SDL_SetError("No current renderer available");
         return 0;
     }
 
@@ -2234,11 +2259,7 @@
 {
     SDL_Renderer *renderer;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2258,11 +2279,7 @@
 {
     SDL_Renderer *renderer;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2286,11 +2303,7 @@
 {
     SDL_Renderer *renderer;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2307,11 +2320,7 @@
 {
     SDL_Renderer *renderer;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2325,11 +2334,7 @@
     SDL_Renderer *renderer;
     SDL_Window *window;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2355,11 +2360,7 @@
         return SDL_RenderPoint(x1, y1);
     }
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2386,11 +2387,7 @@
     SDL_Window *window;
     SDL_Rect real_rect;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return -1;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
         return -1;
     }
@@ -2422,9 +2419,8 @@
     SDL_Rect real_srcrect;
     SDL_Rect real_dstrect;
 
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer) {
-        SDL_SetError("No current renderer available");
         return -1;
     }
     if (!texture) {
@@ -2483,11 +2479,7 @@
 {
     SDL_Renderer *renderer;
 
-    if (!_this) {
-        SDL_UninitializedVideo();
-        return;
-    }
-    renderer = SDL_CurrentDisplay.current_renderer;
+    renderer = SDL_GetCurrentRenderer();
     if (!renderer || !renderer->RenderPresent) {
         return;
     }