Merge lp:~widelands-dev/widelands/editor-grid into lp:widelands

Proposed by Benedikt Straub
Status: Merged
Merged at revision: 9083
Proposed branch: lp:~widelands-dev/widelands/editor-grid
Merge into: lp:widelands
Diff against target: 457 lines (+225/-6)
15 files modified
data/shaders/grid.fp (+5/-0)
data/shaders/grid.vp (+10/-0)
src/editor/editorinteractive.cc (+14/-1)
src/editor/editorinteractive.h (+3/-0)
src/graphic/CMakeLists.txt (+2/-0)
src/graphic/game_renderer.cc (+8/-0)
src/graphic/game_renderer.h (+1/-0)
src/graphic/gl/grid_program.cc (+89/-0)
src/graphic/gl/grid_program.h (+72/-0)
src/graphic/render_queue.cc (+10/-0)
src/graphic/render_queue.h (+3/-0)
src/wui/interactive_player.cc (+1/-1)
src/wui/interactive_spectator.cc (+1/-1)
src/wui/mapview.cc (+2/-1)
src/wui/mapview.h (+4/-2)
To merge this branch: bzr merge lp:~widelands-dev/widelands/editor-grid
Reviewer Review Type Date Requested Status
GunChleoc Approve
Review via email: mp+366505@code.launchpad.net

Commit message

Add a grid overlay to the editor that can be toggled with a button or the hotkey 'G'

Description of the change

Next try :)

I also experimented with draw_line_strip – there was no visible difference but it was a bit slower. So a new, small shader is better IMHO.

To post a comment you must log in.
9023. By GunChleoc

Merged trunk.

9024. By GunChleoc

Fix codecheck.

Revision history for this message
GunChleoc (gunchleoc) wrote :

Performance is definitely an important point!

Reviewed, tested and working :)

@bunnybot merge

review: Approve
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 4802. State: errored. Details: https://travis-ci.org/widelands/widelands/builds/524556788.
Appveyor build 4583. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_editor_grid-4583.

Revision history for this message
bunnybot (widelandsofficial) wrote :

Refusing to merge, since Travis is not green. Use @bunnybot merge force for merging anyways.

Travis build 4802. State: errored. Details: https://travis-ci.org/widelands/widelands/builds/524556788.

Revision history for this message
GunChleoc (gunchleoc) wrote :

Just the usual stalled inputqueues

@bunnybot merge force

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'data/images/wui/menus/menu_toggle_grid.png'
2Binary files data/images/wui/menus/menu_toggle_grid.png 1970-01-01 00:00:00 +0000 and data/images/wui/menus/menu_toggle_grid.png 2019-04-25 16:22:57 +0000 differ
3=== added file 'data/shaders/grid.fp'
4--- data/shaders/grid.fp 1970-01-01 00:00:00 +0000
5+++ data/shaders/grid.fp 2019-04-25 16:22:57 +0000
6@@ -0,0 +1,5 @@
7+#version 120
8+
9+void main() {
10+ gl_FragColor = vec4(.0, .0, .0, .8);
11+}
12
13=== added file 'data/shaders/grid.vp'
14--- data/shaders/grid.vp 1970-01-01 00:00:00 +0000
15+++ data/shaders/grid.vp 2019-04-25 16:22:57 +0000
16@@ -0,0 +1,10 @@
17+#version 120
18+
19+// Attributes.
20+attribute vec2 attr_position;
21+
22+uniform float u_z_value;
23+
24+void main() {
25+ gl_Position = vec4(attr_position, u_z_value, 1.);
26+}
27
28=== modified file 'src/editor/editorinteractive.cc'
29--- src/editor/editorinteractive.cc 2019-04-24 15:56:31 +0000
30+++ src/editor/editorinteractive.cc 2019-04-25 16:22:57 +0000
31@@ -98,6 +98,10 @@
32 toggle_buildhelp_ = add_toolbar_button(
33 "wui/menus/menu_toggle_buildhelp", "buildhelp", _("Show building spaces (on/off)"));
34 toggle_buildhelp_->sigclicked.connect(boost::bind(&EditorInteractive::toggle_buildhelp, this));
35+ toggle_grid_ =
36+ add_toolbar_button("wui/menus/menu_toggle_grid", "grid", _("Show grid (on/off)"));
37+ toggle_grid_->set_perm_pressed(true);
38+ toggle_grid_->sigclicked.connect([this]() { toggle_grid(); });
39 toggle_immovables_ = add_toolbar_button(
40 "wui/menus/menu_toggle_immovables", "immovables", _("Show immovables (on/off)"));
41 toggle_immovables_->set_perm_pressed(true);
42@@ -262,7 +266,7 @@
43
44 void EditorInteractive::draw(RenderTarget& dst) {
45 const auto& ebase = egbase();
46- auto* fields_to_draw = map_view()->draw_terrain(ebase, Workareas(), &dst);
47+ auto* fields_to_draw = map_view()->draw_terrain(ebase, Workareas(), draw_grid_, &dst);
48
49 const float scale = 1.f / map_view()->view().zoom;
50 const uint32_t gametime = ebase.get_gametime();
51@@ -430,6 +434,11 @@
52 toggle_bobs_->set_perm_pressed(draw_bobs_);
53 }
54
55+void EditorInteractive::toggle_grid() {
56+ draw_grid_ = !draw_grid_;
57+ toggle_grid_->set_perm_pressed(draw_grid_);
58+}
59+
60 bool EditorInteractive::handle_key(bool const down, SDL_Keysym const code) {
61 if (down) {
62 switch (code.sym) {
63@@ -504,6 +513,10 @@
64 toggle_buildhelp();
65 return true;
66
67+ case SDLK_g:
68+ toggle_grid();
69+ return true;
70+
71 case SDLK_c:
72 set_display_flag(
73 InteractiveBase::dfShowCensus, !get_display_flag(InteractiveBase::dfShowCensus));
74
75=== modified file 'src/editor/editorinteractive.h'
76--- src/editor/editorinteractive.h 2019-04-24 07:09:29 +0000
77+++ src/editor/editorinteractive.h 2019-04-25 16:22:57 +0000
78@@ -149,6 +149,7 @@
79 void toggle_resources();
80 void toggle_immovables();
81 void toggle_bobs();
82+ void toggle_grid();
83
84 // state variables
85 bool need_save_;
86@@ -170,6 +171,7 @@
87 UI::UniqueWindow::Registry helpmenu_;
88
89 UI::Button* toggle_buildhelp_;
90+ UI::Button* toggle_grid_;
91 UI::Button* toggle_resources_;
92 UI::Button* toggle_immovables_;
93 UI::Button* toggle_bobs_;
94@@ -182,6 +184,7 @@
95 bool draw_resources_ = true;
96 bool draw_immovables_ = true;
97 bool draw_bobs_ = true;
98+ bool draw_grid_ = true;
99 };
100
101 #endif // end of include guard: WL_EDITOR_EDITORINTERACTIVE_H
102
103=== modified file 'src/graphic/CMakeLists.txt'
104--- src/graphic/CMakeLists.txt 2019-04-24 15:56:31 +0000
105+++ src/graphic/CMakeLists.txt 2019-04-25 16:22:57 +0000
106@@ -225,6 +225,8 @@
107
108 wl_library(graphic_terrain_programs
109 SRCS
110+ gl/grid_program.cc
111+ gl/grid_program.h
112 gl/road_program.cc
113 gl/road_program.h
114 gl/terrain_program.cc
115
116=== modified file 'src/graphic/game_renderer.cc'
117--- src/graphic/game_renderer.cc 2019-04-24 15:56:31 +0000
118+++ src/graphic/game_renderer.cc 2019-04-25 16:22:57 +0000
119@@ -61,6 +61,7 @@
120 const FieldsToDraw& fields_to_draw,
121 const float scale,
122 Workareas workarea,
123+ bool grid,
124 RenderTarget* dst) {
125 const Recti& bounding_rect = dst->get_rect();
126 const Surface& surface = dst->get_surface();
127@@ -94,6 +95,13 @@
128 RenderQueue::instance().enqueue(i);
129 }
130
131+ if (grid) {
132+ // Enqueue the drawing of the grid layer.
133+ i.program_id = RenderQueue::Program::kTerrainGrid;
134+ i.blend_mode = BlendMode::UseAlpha;
135+ RenderQueue::instance().enqueue(i);
136+ }
137+
138 // Enqueue the drawing of the road layer.
139 i.program_id = RenderQueue::Program::kTerrainRoad;
140 RenderQueue::instance().enqueue(i);
141
142=== modified file 'src/graphic/game_renderer.h'
143--- src/graphic/game_renderer.h 2019-03-11 14:45:04 +0000
144+++ src/graphic/game_renderer.h 2019-04-25 16:22:57 +0000
145@@ -34,6 +34,7 @@
146 const FieldsToDraw& fields_to_draw,
147 const float scale,
148 Workareas workarea,
149+ bool grid,
150 RenderTarget* dst);
151
152 // Draw the border stones for 'field' if it is a border and 'visibility' is
153
154=== added file 'src/graphic/gl/grid_program.cc'
155--- src/graphic/gl/grid_program.cc 1970-01-01 00:00:00 +0000
156+++ src/graphic/gl/grid_program.cc 2019-04-25 16:22:57 +0000
157@@ -0,0 +1,89 @@
158+/*
159+ * Copyright (C) 2006-2019 by the Widelands Development Team
160+ *
161+ * This program is free software; you can redistribute it and/or
162+ * modify it under the terms of the GNU General Public License
163+ * as published by the Free Software Foundation; either version 2
164+ * of the License, or (at your option) any later version.
165+ *
166+ * This program is distributed in the hope that it will be useful,
167+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
168+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
169+ * GNU General Public License for more details.
170+ *
171+ * You should have received a copy of the GNU General Public License
172+ * along with this program; if not, write to the Free Software
173+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
174+ *
175+ */
176+
177+#include "graphic/gl/grid_program.h"
178+
179+#include "graphic/gl/coordinate_conversion.h"
180+#include "graphic/gl/fields_to_draw.h"
181+#include "graphic/gl/utils.h"
182+#include "graphic/texture.h"
183+
184+GridProgram::GridProgram() {
185+ gl_program_.build("grid");
186+
187+ attr_position_ = glGetAttribLocation(gl_program_.object(), "attr_position");
188+
189+ u_z_value_ = glGetUniformLocation(gl_program_.object(), "u_z_value");
190+}
191+
192+void GridProgram::gl_draw(int gl_texture, float z_value) {
193+ glUseProgram(gl_program_.object());
194+
195+ auto& gl_state = Gl::State::instance();
196+
197+ gl_array_buffer_.bind();
198+ gl_array_buffer_.update(vertices_);
199+
200+ Gl::vertex_attrib_pointer(
201+ attr_position_, 2, sizeof(PerVertexData), offsetof(PerVertexData, gl_x));
202+
203+ gl_state.bind(GL_TEXTURE0, gl_texture);
204+
205+ glUniform1f(u_z_value_, z_value);
206+
207+ glDrawArrays(GL_LINES, 0, vertices_.size());
208+}
209+
210+void GridProgram::add_vertex(const FieldsToDraw::Field& field) {
211+ vertices_.emplace_back();
212+ PerVertexData& back = vertices_.back();
213+ back.gl_x = field.gl_position.x;
214+ back.gl_y = field.gl_position.y;
215+}
216+
217+void GridProgram::draw(uint32_t texture_id,
218+ const FieldsToDraw& fields_to_draw,
219+ float z_value) {
220+ vertices_.clear();
221+ vertices_.reserve(fields_to_draw.size() * 2);
222+
223+ for (size_t current_index = 0; current_index < fields_to_draw.size(); ++current_index) {
224+ const FieldsToDraw::Field& field = fields_to_draw.at(current_index);
225+
226+ // Southwestern edge
227+ if (field.bln_index != FieldsToDraw::kInvalidIndex) {
228+ add_vertex(fields_to_draw.at(current_index));
229+ add_vertex(fields_to_draw.at(field.bln_index));
230+ }
231+
232+ // Southeastern edge
233+ if (field.brn_index != FieldsToDraw::kInvalidIndex) {
234+ add_vertex(fields_to_draw.at(current_index));
235+ add_vertex(fields_to_draw.at(field.brn_index));
236+ }
237+
238+ // Eastern edge
239+ if (field.rn_index != FieldsToDraw::kInvalidIndex) {
240+ add_vertex(fields_to_draw.at(current_index));
241+ add_vertex(fields_to_draw.at(field.rn_index));
242+ }
243+ }
244+
245+ gl_draw(texture_id, z_value);
246+}
247
248=== added file 'src/graphic/gl/grid_program.h'
249--- src/graphic/gl/grid_program.h 1970-01-01 00:00:00 +0000
250+++ src/graphic/gl/grid_program.h 2019-04-25 16:22:57 +0000
251@@ -0,0 +1,72 @@
252+/*
253+ * Copyright (C) 2006-2019 by the Widelands Development Team
254+ *
255+ * This program is free software; you can redistribute it and/or
256+ * modify it under the terms of the GNU General Public License
257+ * as published by the Free Software Foundation; either version 2
258+ * of the License, or (at your option) any later version.
259+ *
260+ * This program is distributed in the hope that it will be useful,
261+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
262+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
263+ * GNU General Public License for more details.
264+ *
265+ * You should have received a copy of the GNU General Public License
266+ * along with this program; if not, write to the Free Software
267+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
268+ *
269+ */
270+
271+#ifndef WL_GRAPHIC_GL_GRID_PROGRAM_H
272+#define WL_GRAPHIC_GL_GRID_PROGRAM_H
273+
274+#include <vector>
275+
276+#include "base/vector.h"
277+#include "graphic/gl/fields_to_draw.h"
278+#include "graphic/gl/utils.h"
279+#include "logic/description_maintainer.h"
280+#include "logic/map_objects/world/terrain_description.h"
281+
282+class GridProgram {
283+public:
284+ // Compiles the program. Throws on errors.
285+ GridProgram();
286+
287+ // Draws the grid layer
288+ void draw(uint32_t texture_id,
289+ const FieldsToDraw& fields_to_draw,
290+ float z_value);
291+
292+private:
293+ struct PerVertexData {
294+ float gl_x;
295+ float gl_y;
296+ };
297+ static_assert(sizeof(PerVertexData) == 8, "Wrong padding.");
298+
299+ void gl_draw(int gl_texture, float z_value);
300+
301+ // Adds a vertex to the end of vertices with data from 'field'.
302+ void add_vertex(const FieldsToDraw::Field& field);
303+
304+ // The program used for drawing the grid layer
305+ Gl::Program gl_program_;
306+
307+ // The buffer that will contain 'vertices_' for rendering.
308+ Gl::Buffer<PerVertexData> gl_array_buffer_;
309+
310+ // Attributes.
311+ GLint attr_position_;
312+
313+ // Uniforms.
314+ GLint u_z_value_;
315+
316+ // Objects below are kept around to avoid memory allocations on each frame.
317+ // They could theoretically also be recreated.
318+ std::vector<PerVertexData> vertices_;
319+
320+ DISALLOW_COPY_AND_ASSIGN(GridProgram);
321+};
322+
323+#endif // end of include guard: WL_GRAPHIC_GL_GRID_PROGRAM_H
324
325=== modified file 'src/graphic/render_queue.cc'
326--- src/graphic/render_queue.cc 2019-04-25 00:03:42 +0000
327+++ src/graphic/render_queue.cc 2019-04-25 16:22:57 +0000
328@@ -28,6 +28,7 @@
329 #include "graphic/gl/dither_program.h"
330 #include "graphic/gl/draw_line_program.h"
331 #include "graphic/gl/fill_rect_program.h"
332+#include "graphic/gl/grid_program.h"
333 #include "graphic/gl/road_program.h"
334 #include "graphic/gl/terrain_program.h"
335 #include "graphic/gl/workarea_program.h"
336@@ -144,6 +145,7 @@
337 terrain_program_(new TerrainProgram()),
338 dither_program_(new DitherProgram()),
339 workarea_program_(new WorkareaProgram()),
340+ grid_program_(new GridProgram()),
341 road_program_(new RoadProgram()) {
342 }
343
344@@ -167,6 +169,7 @@
345 case Program::kTerrainBase:
346 case Program::kTerrainDither:
347 case Program::kTerrainWorkarea:
348+ case Program::kTerrainGrid:
349 case Program::kTerrainRoad:
350 /* all fallthroughs intended */
351 break;
352@@ -262,6 +265,13 @@
353 ++i;
354 } break;
355
356+ case Program::kTerrainGrid: {
357+ ScopedScissor scoped_scissor(item.terrain_arguments.destination_rect);
358+ grid_program_->draw(item.terrain_arguments.terrains->get(0).get_texture(0).blit_data().texture_id,
359+ *item.terrain_arguments.fields_to_draw, item.z_value);
360+ ++i;
361+ } break;
362+
363 case Program::kTerrainRoad: {
364 ScopedScissor scoped_scissor(item.terrain_arguments.destination_rect);
365 road_program_->draw(item.terrain_arguments.renderbuffer_width,
366
367=== modified file 'src/graphic/render_queue.h'
368--- src/graphic/render_queue.h 2019-03-11 14:45:04 +0000
369+++ src/graphic/render_queue.h 2019-04-25 16:22:57 +0000
370@@ -36,6 +36,7 @@
371 #include "logic/map_objects/world/terrain_description.h"
372
373 class DitherProgram;
374+class GridProgram;
375 class RoadProgram;
376 class TerrainProgram;
377 class WorkareaProgram;
378@@ -84,6 +85,7 @@
379 kTerrainBase,
380 kTerrainDither,
381 kTerrainWorkarea,
382+ kTerrainGrid,
383 kTerrainRoad,
384 kBlit,
385 kRect,
386@@ -182,6 +184,7 @@
387 std::unique_ptr<TerrainProgram> terrain_program_;
388 std::unique_ptr<DitherProgram> dither_program_;
389 std::unique_ptr<WorkareaProgram> workarea_program_;
390+ std::unique_ptr<GridProgram> grid_program_;
391 std::unique_ptr<RoadProgram> road_program_;
392
393 std::vector<Item> blended_items_;
394
395=== modified file 'src/wui/interactive_player.cc'
396--- src/wui/interactive_player.cc 2019-04-25 00:03:42 +0000
397+++ src/wui/interactive_player.cc 2019-04-25 16:22:57 +0000
398@@ -288,7 +288,7 @@
399 const uint32_t gametime = gbase.get_gametime();
400
401 Workareas workareas = get_workarea_overlays(map);
402- auto* fields_to_draw = given_map_view->draw_terrain(gbase, workareas, dst);
403+ auto* fields_to_draw = given_map_view->draw_terrain(gbase, workareas, false, dst);
404 const auto& road_building = road_building_overlays();
405
406 const float scale = 1.f / given_map_view->view().zoom;
407
408=== modified file 'src/wui/interactive_spectator.cc'
409--- src/wui/interactive_spectator.cc 2019-04-24 15:56:31 +0000
410+++ src/wui/interactive_spectator.cc 2019-04-25 16:22:57 +0000
411@@ -116,7 +116,7 @@
412
413 const Widelands::Game& the_game = game();
414 const Widelands::Map& map = the_game.map();
415- auto* fields_to_draw = given_map_view->draw_terrain(the_game, get_workarea_overlays(map), dst);
416+ auto* fields_to_draw = given_map_view->draw_terrain(the_game, get_workarea_overlays(map), false, dst);
417 const float scale = 1.f / given_map_view->view().zoom;
418 const uint32_t gametime = the_game.get_gametime();
419
420
421=== modified file 'src/wui/mapview.cc'
422--- src/wui/mapview.cc 2019-04-25 00:03:42 +0000
423+++ src/wui/mapview.cc 2019-04-25 16:22:57 +0000
424@@ -341,6 +341,7 @@
425
426 FieldsToDraw* MapView::draw_terrain(const Widelands::EditorGameBase& egbase,
427 Workareas workarea,
428+ bool grid,
429 RenderTarget* dst) {
430 uint32_t now = SDL_GetTicks();
431 while (!view_plans_.empty()) {
432@@ -385,7 +386,7 @@
433
434 fields_to_draw_.reset(egbase, view_.viewpoint, view_.zoom, dst);
435 const float scale = 1.f / view_.zoom;
436- ::draw_terrain(egbase, fields_to_draw_, scale, workarea, dst);
437+ ::draw_terrain(egbase, fields_to_draw_, scale, workarea, grid, dst);
438 return &fields_to_draw_;
439 }
440
441
442=== modified file 'src/wui/mapview.h'
443--- src/wui/mapview.h 2019-04-25 00:03:42 +0000
444+++ src/wui/mapview.h 2019-04-25 16:22:57 +0000
445@@ -172,8 +172,10 @@
446 // Schedules drawing of the terrain of this MapView. The returned value can
447 // be used to override contents of 'fields_to_draw' for player knowledge and
448 // visibility, and to correctly draw map objects, overlays and text.
449- FieldsToDraw*
450- draw_terrain(const Widelands::EditorGameBase& egbase, Workareas workarea, RenderTarget* dst);
451+ FieldsToDraw* draw_terrain(const Widelands::EditorGameBase& egbase,
452+ Workareas workarea,
453+ bool grid,
454+ RenderTarget* dst);
455
456 // Not overriden from UI::Panel, instead we expect to be passed the data through.
457 bool handle_mousepress(uint8_t btn, int32_t x, int32_t y);

Subscribers

People subscribed via source and target branches

to status/vote changes: