Debian Codegraph @

src/cdogs/grafx_bg.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2014, 2016-2020 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "grafx_bg.h"
29
30 #include "actors.h"
31 #include "ai.h"
32 #include "draw/draw.h"
33 #include "draw/drawtools.h"
34 #include "game_events.h"
35 #include "handle_game_events.h"
36 #include "log.h"
37 #include "map_build.h"
38 #include "objs.h"
39 #include "pickup.h"
40 #include "quick_play.h"
41 #include "texture.h"
42 #include "triggers.h"
43
44 static void DrawBackgroundWithRenderer(
45 GraphicsDevice *g, WindowContext *wc, SDL_Texture *target,
46 SDL_Texture *src, DrawBuffer *buffer, const HSV tint,
47 const struct vec2 pos, const DrawBufferArgs *args);
48 void GrafxDrawBackground(
49 GraphicsDevice *g, DrawBuffer *buffer, const HSV tint,
50 const struct vec2 pos, const DrawBufferArgs *args)
51 {
52 DrawBackgroundWithRenderer(
53 g, &g->gameWindow, g->bkgTgt, g->bkg, buffer, tint, pos, args);
54 if (g->cachedConfig.SecondWindow)
55 {
56 DrawBackgroundWithRenderer(
57 g, &g->secondWindow, g->bkgTgt2, g->bkg2, buffer, tint, pos,
58 args);
59 }
60 }
61 static void DrawBackground(
62 GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
63 const struct vec2 pos, const DrawBufferArgs *args);
64 static void DrawBackgroundWithRenderer(
65 GraphicsDevice *g, WindowContext *wc, SDL_Texture *target,
66 SDL_Texture *src, DrawBuffer *buffer, const HSV tint,
67 const struct vec2 pos, const DrawBufferArgs *args)
68 {
69 SDL_RendererInfo ri;
70 if (SDL_GetRendererInfo(wc->renderer, &ri) != 0)
71 {
72 LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
73 }
74 else
75 {
76 if (!(ri.flags & SDL_RENDERER_TARGETTEXTURE))
77 {
78 LOG(LM_GFX, LL_ERROR,
79 "renderer does not support render to texture");
80 }
81 }
82 if (SDL_SetRenderTarget(wc->renderer, target) != 0)
83 {
84 LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
85 }
86 wc->bkgMask = ColorTint(colorWhite, tint);
87 DrawBackground(g, src, buffer, &gMap, pos, args);
88 if (SDL_SetRenderTarget(wc->renderer, NULL) != 0)
89 {
90 LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
91 }
92 }
93 static void DrawBackground(
94 GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
95 const struct vec2 pos, const DrawBufferArgs *args)
96 {
97 BlitClearBuf(g);
98 DrawBufferSetFromMap(buffer, map, pos, X_TILES);
99 DrawBufferDraw(buffer, svec2i_zero(), args);
100 BlitUpdateFromBuf(g, t);
101 BlitClearBuf(g);
102 }
103
104 void GrafxRedrawBackground(GraphicsDevice *g, const struct vec2 pos)
105 {
106 memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig));
107 DrawBuffer buffer;
108 DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), g);
109 const HSV tint = {rand() * 360.0 / RAND_MAX, rand() * 1.0 / RAND_MAX, 0.5};
110 DrawBufferArgs args;
111 memset(&args, 0, sizeof args);
112 GrafxDrawBackground(g, &buffer, tint, pos, &args);
113 DrawBufferTerminate(&buffer);
114 }
115
116 void GrafxMakeBackground(
117 GraphicsDevice *device, DrawBuffer *buffer, Campaign *co,
118 struct MissionOptions *mo, Map *map, HSV tint, const bool isEditor,
119 struct vec2 pos, const DrawBufferArgs *args)
120 {
121 CampaignAndMissionSetup(co, mo);
122 GameEventsInit(&gGameEvents);
123 MapBuild(map, mo->missionData, true, mo->index, GAME_MODE_NORMAL, &co->Setting.characters);
124 InitializeBadGuys();
125 CreateEnemies();
126 MapMarkAllAsVisited(map);
127 if (isEditor)
128 {
129 for (int i = 0; i < (int)map->exits.size; i++)
130 {
131 MapShowExitArea(map, i);
132 }
133 }
134 else
135 {
136 pos = Vec2CenterOfTile(svec2i_scale_divide(map->Size, 2));
137 }
138 // Process the events that place dynamic objects
139 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
140 GrafxDrawBackground(device, buffer, tint, pos, args);
141 GameEventsTerminate(&gGameEvents);
142 }