| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "window_context.h" |
| 27 |
|
| 28 |
#include "log.h" |
| 29 |
#include "texture.h" |
| 30 |
|
| 31 |
bool WindowContextCreate( |
| 32 |
WindowContext *wc, const Rect2i windowDim, const int windowFlags, |
| 33 |
const char *title, SDL_Surface *icon, |
| 34 |
const struct vec2i rendererLogicalSize) |
| 35 |
{ |
| 36 |
LOG(LM_GFX, LL_DEBUG, "creating window (%X, %X) %dx%d flags(%X)", |
| 37 |
windowDim.Pos.x, windowDim.Pos.y, windowDim.Size.x, windowDim.Size.y, |
| 38 |
windowFlags); |
| 39 |
wc->bkgMask = colorWhite; |
| 40 |
wc->window = SDL_CreateWindow( |
| 41 |
title, windowDim.Pos.x, windowDim.Pos.y, windowDim.Size.x, |
| 42 |
windowDim.Size.y, windowFlags); |
| 43 |
if (wc->window == NULL) |
| 44 |
{ |
| 45 |
LOG(LM_GFX, LL_ERROR, "cannot create window: %s", SDL_GetError()); |
| 46 |
return false; |
| 47 |
} |
| 48 |
wc->renderer = SDL_CreateRenderer( |
| 49 |
wc->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); |
| 50 |
if (wc->renderer == NULL) |
| 51 |
{ |
| 52 |
LOG(LM_GFX, LL_ERROR, "cannot create renderer: %s", SDL_GetError()); |
| 53 |
return false; |
| 54 |
} |
| 55 |
LOG(LM_GFX, LL_DEBUG, "setting icon"); |
| 56 |
SDL_SetWindowIcon(wc->window, icon); |
| 57 |
|
| 58 |
if (!WindowContextInitTextures(wc, rendererLogicalSize)) |
| 59 |
{ |
| 60 |
return false; |
| 61 |
} |
| 62 |
return true; |
| 63 |
} |
| 64 |
bool WindowContextInitTextures( |
| 65 |
WindowContext *wc, const struct vec2i rendererLogicalSize) |
| 66 |
{ |
| 67 |
if (wc->renderer == NULL) |
| 68 |
{ |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
CArrayInit(&wc->texturesBkg, sizeof(SDL_Texture *)); |
| 73 |
CArrayInit(&wc->textures, sizeof(SDL_Texture *)); |
| 74 |
|
| 75 |
if (SDL_RenderSetLogicalSize( |
| 76 |
wc->renderer, rendererLogicalSize.x, rendererLogicalSize.y) != 0) |
| 77 |
{ |
| 78 |
LOG(LM_GFX, LL_ERROR, "cannot set renderer logical size: %s", |
| 79 |
SDL_GetError()); |
| 80 |
return false; |
| 81 |
} |
| 82 |
return true; |
| 83 |
} |
| 84 |
void WindowContextDestroy(WindowContext *wc) |
| 85 |
{ |
| 86 |
WindowContextDestroyTextures(wc); |
| 87 |
SDL_DestroyRenderer(wc->renderer); |
| 88 |
SDL_DestroyWindow(wc->window); |
| 89 |
} |
| 90 |
void WindowContextDestroyTextures(WindowContext *wc) |
| 91 |
{ |
| 92 |
CA_FOREACH(SDL_Texture *, t, wc->texturesBkg) |
| 93 |
SDL_DestroyTexture(*t); |
| 94 |
CA_FOREACH_END() |
| 95 |
CArrayTerminate(&wc->texturesBkg); |
| 96 |
CA_FOREACH(SDL_Texture *, t, wc->textures) |
| 97 |
SDL_DestroyTexture(*t); |
| 98 |
CA_FOREACH_END() |
| 99 |
CArrayTerminate(&wc->textures); |
| 100 |
} |
| 101 |
|
| 102 |
void WindowsAdjustPosition(WindowContext *wc1, WindowContext *wc2) |
| 103 |
{ |
| 104 |
|
| 105 |
struct vec2i pos; |
| 106 |
SDL_GetWindowPosition(wc1->window, &pos.x, &pos.y); |
| 107 |
struct vec2i size; |
| 108 |
SDL_GetWindowSize(wc1->window, &size.x, &size.y); |
| 109 |
|
| 110 |
SDL_SetWindowPosition(wc1->window, pos.x - size.x / 2, pos.y); |
| 111 |
SDL_SetWindowPosition(wc2->window, pos.x + size.x / 2, pos.y); |
| 112 |
} |
| 113 |
|
| 114 |
SDL_Texture *WindowContextCreateTexture( |
| 115 |
WindowContext *wc, const SDL_TextureAccess texAccess, |
| 116 |
const struct vec2i res, const SDL_BlendMode blend, const Uint8 alpha, |
| 117 |
const bool isBkg) |
| 118 |
{ |
| 119 |
SDL_Texture *t = TextureCreate(wc->renderer, texAccess, res, blend, alpha); |
| 120 |
CArrayPushBack(isBkg ? &wc->texturesBkg : &wc->textures, &t); |
| 121 |
return t; |
| 122 |
} |
| 123 |
|
| 124 |
void WindowContextPreRender(WindowContext *wc) |
| 125 |
{ |
| 126 |
if (SDL_SetRenderDrawColor(wc->renderer, 0, 0, 0, 255) != 0) |
| 127 |
{ |
| 128 |
LOG(LM_GFX, LL_ERROR, "Failed to set draw color: %s", SDL_GetError()); |
| 129 |
} |
| 130 |
if (SDL_RenderClear(wc->renderer) != 0) |
| 131 |
{ |
| 132 |
LOG(LM_MAIN, LL_ERROR, "Failed to clear renderer: %s", SDL_GetError()); |
| 133 |
return; |
| 134 |
} |
| 135 |
CA_FOREACH(SDL_Texture *, t, wc->texturesBkg) |
| 136 |
TextureRender( |
| 137 |
*t, wc->renderer, Rect2iZero(), Rect2iZero(), wc->bkgMask, 0, |
| 138 |
SDL_FLIP_NONE); |
| 139 |
CA_FOREACH_END() |
| 140 |
} |
| 141 |
|
| 142 |
void WindowContextPostRender(WindowContext *wc) |
| 143 |
{ |
| 144 |
CA_FOREACH(SDL_Texture *, t, wc->textures) |
| 145 |
TextureRender( |
| 146 |
*t, wc->renderer, Rect2iZero(), Rect2iZero(), colorWhite, 0, |
| 147 |
SDL_FLIP_NONE); |
| 148 |
CA_FOREACH_END() |
| 149 |
|
| 150 |
SDL_RenderPresent(wc->renderer); |
| 151 |
} |