Skip to content

Commit b204bc4

Browse files
committedAug 17, 2017
clientmap, clientmedia: code modernization
* use range-based for loops * simplify some tests * various code style fixes * remove debugprint in ClientMap::getBackgroundBrightness, debug code was not intended to be there * remove unused fields in MapDrawControl * use emplace_back instead of push_back when necessary
1 parent 3e80bf9 commit b204bc4

File tree

5 files changed

+70
-124
lines changed

5 files changed

+70
-124
lines changed
 

Diff for: ‎src/clientmap.cpp

+28-69
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include "mapblock_mesh.h"
2323
#include <IMaterialRenderer.h>
2424
#include <matrix4.h>
25-
#include "log.h"
2625
#include "mapsector.h"
27-
#include "nodedef.h"
2826
#include "mapblock.h"
2927
#include "profiler.h"
3028
#include "settings.h"
@@ -42,10 +40,7 @@ ClientMap::ClientMap(
4240
scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
4341
RenderingEngine::get_scene_manager(), id),
4442
m_client(client),
45-
m_control(control),
46-
m_camera_position(0,0,0),
47-
m_camera_direction(0,0,1),
48-
m_camera_fov(M_PI)
43+
m_control(control)
4944
{
5045
m_box = aabb3f(-BS*1000000,-BS*1000000,-BS*1000000,
5146
BS*1000000,BS*1000000,BS*1000000);
@@ -65,10 +60,6 @@ ClientMap::ClientMap(
6560

6661
}
6762

68-
ClientMap::~ClientMap()
69-
{
70-
}
71-
7263
MapSector * ClientMap::emergeSector(v2s16 p2d)
7364
{
7465
DSTACK(FUNCTION_NAME);
@@ -129,14 +120,13 @@ void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
129120
p_nodes_max.Z / MAP_BLOCKSIZE + 1);
130121
}
131122

132-
void ClientMap::updateDrawList(video::IVideoDriver* driver)
123+
void ClientMap::updateDrawList()
133124
{
134125
ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
135126
g_profiler->add("CM::updateDrawList() count", 1);
136127

137-
for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
138-
i != m_drawlist.end(); ++i) {
139-
MapBlock *block = i->second;
128+
for (auto &i : m_drawlist) {
129+
MapBlock *block = i.second;
140130
block->refDrop();
141131
}
142132
m_drawlist.clear();
@@ -183,12 +173,11 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
183173
occlusion_culling_enabled = false;
184174
}
185175

186-
for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
187-
si != m_sectors.end(); ++si) {
188-
MapSector *sector = si->second;
176+
for (const auto &sector_it : m_sectors) {
177+
MapSector *sector = sector_it.second;
189178
v2s16 sp = sector->getPos();
190179

191-
if (m_control.range_all == false) {
180+
if (!m_control.range_all) {
192181
if (sp.X < p_blocks_min.X || sp.X > p_blocks_max.X ||
193182
sp.Y < p_blocks_min.Z || sp.Y > p_blocks_max.Z)
194183
continue;
@@ -203,14 +192,11 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
203192

204193
u32 sector_blocks_drawn = 0;
205194

206-
for (MapBlockVect::iterator i = sectorblocks.begin();
207-
i != sectorblocks.end(); ++i) {
208-
MapBlock *block = *i;
209-
195+
for (auto block : sectorblocks) {
210196
/*
211-
Compare block position to camera position, skip
212-
if not seen on display
213-
*/
197+
Compare block position to camera position, skip
198+
if not seen on display
199+
*/
214200

215201
if (block->mesh)
216202
block->mesh->updateCameraOffset(m_camera_offset);
@@ -267,10 +253,6 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
267253
m_last_drawn_sectors.insert(sp);
268254
}
269255

270-
m_control.blocks_would_have_drawn = blocks_would_have_drawn;
271-
m_control.blocks_drawn = blocks_drawn;
272-
m_control.farthest_drawn = farthest_drawn;
273-
274256
g_profiler->avg("CM: blocks in range", blocks_in_range);
275257
g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
276258
if (blocks_in_range != 0)
@@ -389,9 +371,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
389371

390372
MeshBufListList drawbufs;
391373

392-
for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
393-
i != m_drawlist.end(); ++i) {
394-
MapBlock *block = i->second;
374+
for (auto &i : m_drawlist) {
375+
MapBlock *block = i.second;
395376

396377
// If the mesh of the block happened to get deleted, ignore it
397378
if (!block->mesh)
@@ -507,12 +488,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
507488
if (blocks_drawn != 0)
508489
g_profiler->avg(prefix + "empty blocks (frac)",
509490
(float)blocks_without_stuff / blocks_drawn);
510-
511-
/*infostream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
512-
<<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
513491
}
514492

515-
static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
493+
static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
516494
float step_multiplier, float start_distance, float end_distance,
517495
INodeDefManager *ndef, u32 daylight_factor, float sunlight_min_d,
518496
int *result, bool *sunlight_seen)
@@ -546,19 +524,20 @@ static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
546524
sunlight_min_d = 0;
547525
}
548526
}
549-
for(int i=0; distance < end_distance; i++){
527+
for (int i=0; distance < end_distance; i++) {
550528
pf += dir * step;
551529
distance += step;
552530
step *= step_multiplier;
553531

554532
v3s16 p = floatToInt(pf, BS);
555533
MapNode n = map->getNodeNoEx(p);
556-
if(allow_allowing_non_sunlight_propagates && i == 0 &&
534+
if (allow_allowing_non_sunlight_propagates && i == 0 &&
557535
ndef->get(n).param_type == CPT_LIGHT &&
558-
!ndef->get(n).sunlight_propagates){
536+
!ndef->get(n).sunlight_propagates) {
559537
allow_non_sunlight_propagates = true;
560538
}
561-
if(ndef->get(n).param_type != CPT_LIGHT ||
539+
540+
if (ndef->get(n).param_type != CPT_LIGHT ||
562541
(!ndef->get(n).sunlight_propagates &&
563542
!allow_non_sunlight_propagates)){
564543
nonlight_seen = true;
@@ -567,9 +546,9 @@ static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
567546
break;
568547
continue;
569548
}
570-
if(distance >= sunlight_min_d && *sunlight_seen == false
571-
&& nonlight_seen == false)
572-
if(n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
549+
550+
if (distance >= sunlight_min_d && !*sunlight_seen && !nonlight_seen)
551+
if (n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
573552
*sunlight_seen = true;
574553
noncount = 0;
575554
brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef));
@@ -587,13 +566,13 @@ static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
587566
int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
588567
int oldvalue, bool *sunlight_seen_result)
589568
{
590-
const bool debugprint = false;
591569
static v3f z_directions[50] = {
592570
v3f(-100, 0, 0)
593571
};
594572
static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
595573
-1000,
596574
};
575+
597576
if(z_directions[0].X < -99){
598577
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
599578
// Assumes FOV of 72 and 16/9 aspect ratio
@@ -605,8 +584,7 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
605584
z_offsets[i] = 0.01 * myrand_range(0,100);
606585
}
607586
}
608-
if(debugprint)
609-
std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes ";
587+
610588
int sunlight_seen_count = 0;
611589
float sunlight_min_d = max_d*0.8;
612590
if(sunlight_min_d > 35*BS)
@@ -646,22 +624,12 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
646624
else if(num_values_to_use >= 7)
647625
num_values_to_use -= num_values_to_use/3;
648626
u32 first_value_i = (values.size() - num_values_to_use) / 2;
649-
if(debugprint){
650-
for(u32 i=0; i < first_value_i; i++)
651-
std::cerr<<values[i]<<" ";
652-
std::cerr<<"[";
653-
}
654-
for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){
655-
if(debugprint)
656-
std::cerr<<values[i]<<" ";
627+
628+
for (u32 i=first_value_i; i < first_value_i + num_values_to_use; i++) {
657629
brightness_sum += values[i];
658630
brightness_count++;
659631
}
660-
if(debugprint){
661-
std::cerr<<"]";
662-
for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++)
663-
std::cerr<<values[i]<<" ";
664-
}
632+
665633
int ret = 0;
666634
if(brightness_count == 0){
667635
MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
@@ -671,18 +639,9 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
671639
ret = oldvalue;
672640
}
673641
} else {
674-
/*float pre = (float)brightness_sum / (float)brightness_count;
675-
float tmp = pre;
676-
const float d = 0.2;
677-
pre *= 1.0 + d*2;
678-
pre -= tmp * d;
679-
int preint = pre;
680-
ret = MYMAX(0, MYMIN(255, preint));*/
681642
ret = brightness_sum / brightness_count;
682643
}
683-
if(debugprint)
684-
std::cerr<<"Result: "<<ret<<" sunlight_seen_count="
685-
<<sunlight_seen_count<<std::endl;
644+
686645
*sunlight_seen_result = (sunlight_seen_count > 0);
687646
return ret;
688647
}

Diff for: ‎src/clientmap.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ struct MapDrawControl
3636
u32 wanted_max_blocks = 0;
3737
// show a wire frame for debugging
3838
bool show_wireframe = false;
39-
// Number of blocks rendered is written here by the renderer
40-
u32 blocks_drawn = 0;
41-
// Number of blocks that would have been drawn in wanted_range
42-
u32 blocks_would_have_drawn = 0;
43-
// Distance to the farthest block drawn
44-
float farthest_drawn = 0;
4539
};
4640

4741
class Client;
@@ -62,7 +56,7 @@ class ClientMap : public Map, public scene::ISceneNode
6256
s32 id
6357
);
6458

65-
~ClientMap();
59+
virtual ~ClientMap() = default;
6660

6761
s32 mapType() const
6862
{
@@ -74,7 +68,7 @@ class ClientMap : public Map, public scene::ISceneNode
7468
ISceneNode::drop();
7569
}
7670

77-
void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset)
71+
void updateCamera(const v3f &pos, const v3f &dir, f32 fov, const v3s16 &offset)
7872
{
7973
m_camera_position = pos;
8074
m_camera_direction = dir;
@@ -109,7 +103,7 @@ class ClientMap : public Map, public scene::ISceneNode
109103

110104
void getBlocksInViewRange(v3s16 cam_pos_nodes,
111105
v3s16 *p_blocks_min, v3s16 *p_blocks_max);
112-
void updateDrawList(video::IVideoDriver* driver);
106+
void updateDrawList();
113107
void renderMap(video::IVideoDriver* driver, s32 pass);
114108

115109
int getBackgroundBrightness(float max_d, u32 daylight_factor,
@@ -130,7 +124,7 @@ class ClientMap : public Map, public scene::ISceneNode
130124

131125
MapDrawControl &m_control;
132126

133-
v3f m_camera_position;
127+
v3f m_camera_position = v3f(0,0,0);
134128
v3f m_camera_direction = v3f(0,0,1);
135129
f32 m_camera_fov = M_PI;
136130
v3s16 m_camera_offset;

0 commit comments

Comments
 (0)
Please sign in to comment.