comparison 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
comparison
equal deleted inserted replaced
3416:3d50171ac879 3417:64a60c5d502e
752 SDL_Window window; 752 SDL_Window window;
753 int num_windows; 753 int num_windows;
754 SDL_Window *windows; 754 SDL_Window *windows;
755 755
756 if (!_this) { 756 if (!_this) {
757 SDL_UninitializedVideo(); 757 /* Initialize the video system if needed */
758 return 0; 758 if (SDL_VideoInit(NULL, 0) < 0) {
759 return 0;
760 }
759 } 761 }
760 if (flags & SDL_WINDOW_OPENGL) { 762 if (flags & SDL_WINDOW_OPENGL) {
761 if (!_this->GL_CreateContext) { 763 if (!_this->GL_CreateContext) {
762 SDL_SetError("No OpenGL support in video driver"); 764 SDL_SetError("No OpenGL support in video driver");
763 return 0; 765 return 0;
924 926
925 if (!_this) { 927 if (!_this) {
926 SDL_UninitializedVideo(); 928 SDL_UninitializedVideo();
927 return NULL; 929 return NULL;
928 } 930 }
929 for (i = 0; i < _this->num_displays; ++i) { 931 if (windowID) {
930 SDL_VideoDisplay *display = &_this->displays[i]; 932 for (i = 0; i < _this->num_displays; ++i) {
931 for (j = 0; j < display->num_windows; ++j) { 933 SDL_VideoDisplay *display = &_this->displays[i];
932 SDL_Window *window = &display->windows[j]; 934 for (j = 0; j < display->num_windows; ++j) {
933 if (window->id == windowID) { 935 SDL_Window *window = &display->windows[j];
936 if (window->id == windowID) {
937 return window;
938 }
939 }
940 }
941 } else {
942 /* Just return the first active window */
943 for (i = 0; i < _this->num_displays; ++i) {
944 SDL_VideoDisplay *display = &_this->displays[i];
945 for (j = 0; j < display->num_windows; ++j) {
946 SDL_Window *window = &display->windows[j];
934 return window; 947 return window;
935 } 948 }
936 } 949 }
937 } 950 }
951 /* Couldn't find the window with the requested ID */
952 SDL_SetError("Invalid window ID");
938 return NULL; 953 return NULL;
939 } 954 }
940 955
941 SDL_VideoDisplay * 956 SDL_VideoDisplay *
942 SDL_GetDisplayFromWindow(SDL_Window * window) 957 SDL_GetDisplayFromWindow(SDL_Window * window)
947 } 962 }
948 if (!window) { 963 if (!window) {
949 return NULL; 964 return NULL;
950 } 965 }
951 return &_this->displays[window->display]; 966 return &_this->displays[window->display];
967 }
968
969 static __inline__ SDL_Renderer *
970 SDL_GetCurrentRenderer()
971 {
972 SDL_Renderer *renderer;
973
974 if (!_this) {
975 SDL_UninitializedVideo();
976 return NULL;
977 }
978 if (!SDL_CurrentRenderer) {
979 if (SDL_CreateRenderer(0, -1, 0) < 0) {
980 return NULL;
981 }
982 }
983 return SDL_CurrentRenderer;
952 } 984 }
953 985
954 Uint32 986 Uint32
955 SDL_GetWindowFlags(SDL_WindowID windowID) 987 SDL_GetWindowFlags(SDL_WindowID windowID)
956 { 988 {
1504 if (!window) { 1536 if (!window) {
1505 SDL_SetError("Invalid window ID"); 1537 SDL_SetError("Invalid window ID");
1506 return -1; 1538 return -1;
1507 } 1539 }
1508 renderer = window->renderer; 1540 renderer = window->renderer;
1541 if (renderer) {
1542 if (renderer->ActivateRenderer) {
1543 if (renderer->ActivateRenderer(renderer) < 0) {
1544 return -1;
1545 }
1546 }
1547 SDL_CurrentDisplay.current_renderer = renderer;
1548 } else {
1549 if (SDL_CreateRenderer(windowID, -1, 0) < 0) {
1550 return -1;
1551 }
1552 }
1553 return 0;
1554 }
1555
1556 int
1557 SDL_GetRendererInfo(SDL_RendererInfo * info)
1558 {
1559 SDL_Renderer *renderer = SDL_GetCurrentRenderer();
1509 if (!renderer) { 1560 if (!renderer) {
1510 SDL_SetError("Renderer hasn't been created yet"); 1561 return -1;
1511 return -1; 1562 }
1512 } 1563 *info = renderer->info;
1513 if (renderer->ActivateRenderer) {
1514 if (renderer->ActivateRenderer(renderer) < 0) {
1515 return -1;
1516 }
1517 }
1518 SDL_CurrentDisplay.current_renderer = renderer;
1519 return 0;
1520 }
1521
1522 int
1523 SDL_GetRendererInfo(SDL_RendererInfo * info)
1524 {
1525 if (!_this) {
1526 SDL_UninitializedVideo();
1527 return -1;
1528 }
1529 if (!SDL_CurrentDisplay.current_renderer) {
1530 SDL_SetError("There is no current renderer");
1531 return -1;
1532 }
1533 *info = SDL_CurrentDisplay.current_renderer->info;
1534 return 0; 1564 return 0;
1535 } 1565 }
1536 1566
1537 SDL_TextureID 1567 SDL_TextureID
1538 SDL_CreateTexture(Uint32 format, int access, int w, int h) 1568 SDL_CreateTexture(Uint32 format, int access, int w, int h)
1539 { 1569 {
1540 int hash; 1570 int hash;
1541 SDL_Renderer *renderer; 1571 SDL_Renderer *renderer;
1542 SDL_Texture *texture; 1572 SDL_Texture *texture;
1543 1573
1544 if (!_this) { 1574 renderer = SDL_GetCurrentRenderer();
1545 SDL_UninitializedVideo();
1546 return 0;
1547 }
1548 renderer = SDL_CurrentDisplay.current_renderer;
1549 if (!renderer) { 1575 if (!renderer) {
1550 return 0; 1576 return 0;
1551 } 1577 }
1552 if (!renderer->CreateTexture) { 1578 if (!renderer->CreateTexture) {
1553 SDL_Unsupported(); 1579 SDL_Unsupported();
1597 SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface"); 1623 SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
1598 return 0; 1624 return 0;
1599 } 1625 }
1600 fmt = surface->format; 1626 fmt = surface->format;
1601 1627
1602 renderer = SDL_CurrentDisplay.current_renderer; 1628 renderer = SDL_GetCurrentRenderer();
1603 if (!renderer) { 1629 if (!renderer) {
1604 SDL_SetError("No current renderer available");
1605 return 0; 1630 return 0;
1606 } 1631 }
1607 1632
1608 if (format) { 1633 if (format) {
1609 if (!SDL_PixelFormatEnumToMasks 1634 if (!SDL_PixelFormatEnumToMasks
2232 int 2257 int
2233 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) 2258 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
2234 { 2259 {
2235 SDL_Renderer *renderer; 2260 SDL_Renderer *renderer;
2236 2261
2237 if (!_this) { 2262 renderer = SDL_GetCurrentRenderer();
2238 SDL_UninitializedVideo();
2239 return -1;
2240 }
2241 renderer = SDL_CurrentDisplay.current_renderer;
2242 if (!renderer) { 2263 if (!renderer) {
2243 return -1; 2264 return -1;
2244 } 2265 }
2245 renderer->r = r; 2266 renderer->r = r;
2246 renderer->g = g; 2267 renderer->g = g;
2256 int 2277 int
2257 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) 2278 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
2258 { 2279 {
2259 SDL_Renderer *renderer; 2280 SDL_Renderer *renderer;
2260 2281
2261 if (!_this) { 2282 renderer = SDL_GetCurrentRenderer();
2262 SDL_UninitializedVideo();
2263 return -1;
2264 }
2265 renderer = SDL_CurrentDisplay.current_renderer;
2266 if (!renderer) { 2283 if (!renderer) {
2267 return -1; 2284 return -1;
2268 } 2285 }
2269 if (r) { 2286 if (r) {
2270 *r = renderer->r; 2287 *r = renderer->r;
2284 int 2301 int
2285 SDL_SetRenderDrawBlendMode(int blendMode) 2302 SDL_SetRenderDrawBlendMode(int blendMode)
2286 { 2303 {
2287 SDL_Renderer *renderer; 2304 SDL_Renderer *renderer;
2288 2305
2289 if (!_this) { 2306 renderer = SDL_GetCurrentRenderer();
2290 SDL_UninitializedVideo();
2291 return -1;
2292 }
2293 renderer = SDL_CurrentDisplay.current_renderer;
2294 if (!renderer) { 2307 if (!renderer) {
2295 return -1; 2308 return -1;
2296 } 2309 }
2297 renderer->blendMode = blendMode; 2310 renderer->blendMode = blendMode;
2298 if (renderer->SetDrawBlendMode) { 2311 if (renderer->SetDrawBlendMode) {
2305 int 2318 int
2306 SDL_GetRenderDrawBlendMode(int *blendMode) 2319 SDL_GetRenderDrawBlendMode(int *blendMode)
2307 { 2320 {
2308 SDL_Renderer *renderer; 2321 SDL_Renderer *renderer;
2309 2322
2310 if (!_this) { 2323 renderer = SDL_GetCurrentRenderer();
2311 SDL_UninitializedVideo();
2312 return -1;
2313 }
2314 renderer = SDL_CurrentDisplay.current_renderer;
2315 if (!renderer) { 2324 if (!renderer) {
2316 return -1; 2325 return -1;
2317 } 2326 }
2318 *blendMode = renderer->blendMode; 2327 *blendMode = renderer->blendMode;
2319 return 0; 2328 return 0;
2323 SDL_RenderPoint(int x, int y) 2332 SDL_RenderPoint(int x, int y)
2324 { 2333 {
2325 SDL_Renderer *renderer; 2334 SDL_Renderer *renderer;
2326 SDL_Window *window; 2335 SDL_Window *window;
2327 2336
2328 if (!_this) { 2337 renderer = SDL_GetCurrentRenderer();
2329 SDL_UninitializedVideo();
2330 return -1;
2331 }
2332 renderer = SDL_CurrentDisplay.current_renderer;
2333 if (!renderer) { 2338 if (!renderer) {
2334 return -1; 2339 return -1;
2335 } 2340 }
2336 if (!renderer->RenderPoint) { 2341 if (!renderer->RenderPoint) {
2337 SDL_Unsupported(); 2342 SDL_Unsupported();
2353 2358
2354 if (x1 == x2 && y1 == y2) { 2359 if (x1 == x2 && y1 == y2) {
2355 return SDL_RenderPoint(x1, y1); 2360 return SDL_RenderPoint(x1, y1);
2356 } 2361 }
2357 2362
2358 if (!_this) { 2363 renderer = SDL_GetCurrentRenderer();
2359 SDL_UninitializedVideo();
2360 return -1;
2361 }
2362 renderer = SDL_CurrentDisplay.current_renderer;
2363 if (!renderer) { 2364 if (!renderer) {
2364 return -1; 2365 return -1;
2365 } 2366 }
2366 if (!renderer->RenderLine) { 2367 if (!renderer->RenderLine) {
2367 SDL_Unsupported(); 2368 SDL_Unsupported();
2384 { 2385 {
2385 SDL_Renderer *renderer; 2386 SDL_Renderer *renderer;
2386 SDL_Window *window; 2387 SDL_Window *window;
2387 SDL_Rect real_rect; 2388 SDL_Rect real_rect;
2388 2389
2389 if (!_this) { 2390 renderer = SDL_GetCurrentRenderer();
2390 SDL_UninitializedVideo();
2391 return -1;
2392 }
2393 renderer = SDL_CurrentDisplay.current_renderer;
2394 if (!renderer) { 2391 if (!renderer) {
2395 return -1; 2392 return -1;
2396 } 2393 }
2397 if (!renderer->RenderFill) { 2394 if (!renderer->RenderFill) {
2398 SDL_Unsupported(); 2395 SDL_Unsupported();
2420 SDL_Renderer *renderer; 2417 SDL_Renderer *renderer;
2421 SDL_Window *window; 2418 SDL_Window *window;
2422 SDL_Rect real_srcrect; 2419 SDL_Rect real_srcrect;
2423 SDL_Rect real_dstrect; 2420 SDL_Rect real_dstrect;
2424 2421
2425 renderer = SDL_CurrentDisplay.current_renderer; 2422 renderer = SDL_GetCurrentRenderer();
2426 if (!renderer) { 2423 if (!renderer) {
2427 SDL_SetError("No current renderer available");
2428 return -1; 2424 return -1;
2429 } 2425 }
2430 if (!texture) { 2426 if (!texture) {
2431 SDL_SetError("Texture not found"); 2427 SDL_SetError("Texture not found");
2432 return -1; 2428 return -1;
2481 void 2477 void
2482 SDL_RenderPresent(void) 2478 SDL_RenderPresent(void)
2483 { 2479 {
2484 SDL_Renderer *renderer; 2480 SDL_Renderer *renderer;
2485 2481
2486 if (!_this) { 2482 renderer = SDL_GetCurrentRenderer();
2487 SDL_UninitializedVideo();
2488 return;
2489 }
2490 renderer = SDL_CurrentDisplay.current_renderer;
2491 if (!renderer || !renderer->RenderPresent) { 2483 if (!renderer || !renderer->RenderPresent) {
2492 return; 2484 return;
2493 } 2485 }
2494 renderer->RenderPresent(renderer); 2486 renderer->RenderPresent(renderer);
2495 } 2487 }