about summary refs log tree commit diff
path: root/src/game/client/components/maplayers.cpp
blob: 75f915214b10cb3e8a7efc0e0dc5200a0292b932 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <engine/client/graphics.h>

#include <game/layers.hpp>
#include <game/client/gameclient.hpp>
#include <game/client/component.hpp>
#include <game/client/render.hpp>

#include <game/client/components/camera.hpp>
#include <game/client/components/mapimages.hpp>


#include "maplayers.hpp"

MAPLAYERS::MAPLAYERS(int t)
{
	type = t;
}


void MAPLAYERS::mapscreen_to_group(float center_x, float center_y, MAPITEM_GROUP *group)
{
	float points[4];
	RenderTools()->mapscreen_to_world(center_x, center_y, group->parallax_x/100.0f, group->parallax_y/100.0f,
		group->offset_x, group->offset_y, Graphics()->ScreenAspect(), 1.0f, points);
	Graphics()->MapScreen(points[0], points[1], points[2], points[3]);
}

void MAPLAYERS::envelope_eval(float time_offset, int env, float *channels, void *user)
{
	MAPLAYERS *pThis = (MAPLAYERS *)user;
	channels[0] = 0;
	channels[1] = 0;
	channels[2] = 0;
	channels[3] = 0;

	ENVPOINT *points;

	{
		int start, num;
		map_get_type(MAPITEMTYPE_ENVPOINTS, &start, &num);
		if(num)
			points = (ENVPOINT *)map_get_item(start, 0, 0);
	}
	
	int start, num;
	map_get_type(MAPITEMTYPE_ENVELOPE, &start, &num);
	
	if(env >= num)
		return;
	
	MAPITEM_ENVELOPE *item = (MAPITEM_ENVELOPE *)map_get_item(start+env, 0, 0);
	pThis->RenderTools()->render_eval_envelope(points+item->start_point, item->num_points, 4, client_localtime()+time_offset, channels);
}

void MAPLAYERS::on_render()
{
	if(client_state() != CLIENTSTATE_ONLINE && client_state() != CLIENTSTATE_DEMOPLAYBACK)
		return;
	
	CUIRect screen;
	Graphics()->GetScreen(&screen.x, &screen.y, &screen.w, &screen.h);
	
	vec2 center = gameclient.camera->center;
	//float center_x = gameclient.camera->center.x;
	//float center_y = gameclient.camera->center.y;
	
	bool passed_gamelayer = false;
	
	for(int g = 0; g < layers_num_groups(); g++)
	{
		MAPITEM_GROUP *group = layers_get_group(g);
		
		if(!config.gfx_noclip && group->version >= 2 && group->use_clipping)
		{
			// set clipping
			float points[4];
			mapscreen_to_group(center.x, center.y, layers_game_group());
			Graphics()->GetScreen(&points[0], &points[1], &points[2], &points[3]);
			float x0 = (group->clip_x - points[0]) / (points[2]-points[0]);
			float y0 = (group->clip_y - points[1]) / (points[3]-points[1]);
			float x1 = ((group->clip_x+group->clip_w) - points[0]) / (points[2]-points[0]);
			float y1 = ((group->clip_y+group->clip_h) - points[1]) / (points[3]-points[1]);
			
			Graphics()->ClipEnable((int)(x0*Graphics()->ScreenWidth()), (int)(y0*Graphics()->ScreenHeight()),
				(int)((x1-x0)*Graphics()->ScreenWidth()), (int)((y1-y0)*Graphics()->ScreenHeight()));
		}		
		
		mapscreen_to_group(center.x, center.y, group);
		
		for(int l = 0; l < group->num_layers; l++)
		{
			MAPITEM_LAYER *layer = layers_get_layer(group->start_layer+l);
			bool render = false;
			bool is_game_layer = false;
			
			if(layer == (MAPITEM_LAYER*)layers_game_layer())
			{
				is_game_layer = true;
				passed_gamelayer = 1;
			}
			
			// skip rendering if detail layers if not wanted
			if(layer->flags&LAYERFLAG_DETAIL && !config.gfx_high_detail && !is_game_layer)
				continue;
				
			if(type == -1)
				render = true;
			else if(type == 0)
			{
				if(passed_gamelayer)
					return;
				render = true;
			}
			else
			{
				if(passed_gamelayer && !is_game_layer)
					render = true;
			}
			
			if(render && !is_game_layer)
			{
				//layershot_begin();
				
				if(layer->type == LAYERTYPE_TILES)
				{
					MAPITEM_LAYER_TILEMAP *tmap = (MAPITEM_LAYER_TILEMAP *)layer;
					if(tmap->image == -1)
						Graphics()->TextureSet(-1);
					else
						Graphics()->TextureSet(gameclient.mapimages->get(tmap->image));
						
					TILE *tiles = (TILE *)map_get_data(tmap->data);
					Graphics()->BlendNone();
					RenderTools()->render_tilemap(tiles, tmap->width, tmap->height, 32.0f, vec4(1,1,1,1), TILERENDERFLAG_EXTEND|LAYERRENDERFLAG_OPAQUE);
					Graphics()->BlendNormal();
					RenderTools()->render_tilemap(tiles, tmap->width, tmap->height, 32.0f, vec4(1,1,1,1), TILERENDERFLAG_EXTEND|LAYERRENDERFLAG_TRANSPARENT);
				}
				else if(layer->type == LAYERTYPE_QUADS)
				{
					MAPITEM_LAYER_QUADS *qlayer = (MAPITEM_LAYER_QUADS *)layer;
					if(qlayer->image == -1)
						Graphics()->TextureSet(-1);
					else
						Graphics()->TextureSet(gameclient.mapimages->get(qlayer->image));

					QUAD *quads = (QUAD *)map_get_data_swapped(qlayer->data);
					
					Graphics()->BlendNone();
					RenderTools()->render_quads(quads, qlayer->num_quads, LAYERRENDERFLAG_OPAQUE, envelope_eval, this);
					Graphics()->BlendNormal();
					RenderTools()->render_quads(quads, qlayer->num_quads, LAYERRENDERFLAG_TRANSPARENT, envelope_eval, this);
				}
				
				//layershot_end();	
			}
		}
		if(!config.gfx_noclip)
			Graphics()->ClipDisable();
	}
	
	if(!config.gfx_noclip)
		Graphics()->ClipDisable();
	
	// reset the screen like it was before
	Graphics()->MapScreen(screen.x, screen.y, screen.w, screen.h);
}