Debian Codegraph @

src/cdogs/window_context.c

raw symbols SCIP
1 /*
2 Copyright (c) 2017-2020 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
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 // Adjust windows so that they are side-by-side
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 }