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, GrafxDrawExtra *extra);
GrafxDrawBackground(GraphicsDevice * g,DrawBuffer * buffer,const HSV tint,const struct vec2 pos,GrafxDrawExtra * extra)48 void GrafxDrawBackground(
49 	GraphicsDevice *g, DrawBuffer *buffer, const HSV tint,
50 	const struct vec2 pos, GrafxDrawExtra *extra)
51 {
52 	DrawBackgroundWithRenderer(
53 		g, &g->gameWindow, g->bkgTgt, g->bkg, buffer, tint, pos, extra);
54 	if (g->cachedConfig.SecondWindow)
55 	{
56 		DrawBackgroundWithRenderer(
57 			g, &g->secondWindow, g->bkgTgt2, g->bkg2, buffer, tint, pos,
58 			extra);
59 	}
60 }
61 static void DrawBackground(
62 	GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
63 	const struct vec2 pos, GrafxDrawExtra *extra);
DrawBackgroundWithRenderer(GraphicsDevice * g,WindowContext * wc,SDL_Texture * target,SDL_Texture * src,DrawBuffer * buffer,const HSV tint,const struct vec2 pos,GrafxDrawExtra * extra)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, GrafxDrawExtra *extra)
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, extra);
88 	if (SDL_SetRenderTarget(wc->renderer, NULL) != 0)
89 	{
90 		LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
91 	}
92 }
DrawBackground(GraphicsDevice * g,SDL_Texture * t,DrawBuffer * buffer,Map * map,const struct vec2 pos,GrafxDrawExtra * extra)93 static void DrawBackground(
94 	GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
95 	const struct vec2 pos, GrafxDrawExtra *extra)
96 {
97 	BlitClearBuf(g);
98 	DrawBufferSetFromMap(buffer, map, pos, X_TILES);
99 	DrawBufferDraw(buffer, svec2i_zero(), extra);
100 	BlitUpdateFromBuf(g, t);
101 	BlitClearBuf(g);
102 }
103 
GrafxRedrawBackground(GraphicsDevice * g,const struct vec2 pos)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 	GrafxDrawBackground(g, &buffer, tint, pos, NULL);
111 	DrawBufferTerminate(&buffer);
112 }
113 
GrafxMakeBackground(GraphicsDevice * device,DrawBuffer * buffer,Campaign * co,struct MissionOptions * mo,Map * map,HSV tint,const bool isEditor,struct vec2 pos,GrafxDrawExtra * extra)114 void GrafxMakeBackground(
115 	GraphicsDevice *device, DrawBuffer *buffer, Campaign *co,
116 	struct MissionOptions *mo, Map *map, HSV tint, const bool isEditor,
117 	struct vec2 pos, GrafxDrawExtra *extra)
118 {
119 	CampaignAndMissionSetup(co, mo);
120 	GameEventsInit(&gGameEvents);
121 	MapBuild(map, mo->missionData, co, mo->index);
122 	InitializeBadGuys();
123 	CreateEnemies();
124 	MapMarkAllAsVisited(map);
125 	if (isEditor)
126 	{
127 		for (int i = 0; i < (int)map->exits.size; i++)
128 		{
129 			MapShowExitArea(map, i);
130 		}
131 	}
132 	else
133 	{
134 		pos = Vec2CenterOfTile(svec2i_scale_divide(map->Size, 2));
135 	}
136 	// Process the events that place dynamic objects
137 	HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
138 	GrafxDrawBackground(device, buffer, tint, pos, extra);
139 	GameEventsTerminate(&gGameEvents);
140 }
141