summary refs log tree commit diff stats
path: root/src/editor.cpp
blob: 8ca850a69aa006541ad8ec2e8623a9357a51a1d0 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "editor.h"
#include "consts.h"

void Editor::tick(
  double dt,
  const Uint8* keystate)
{
  if (keystate[SDL_SCANCODE_LEFT] ||
      keystate[SDL_SCANCODE_UP] ||
      keystate[SDL_SCANCODE_RIGHT] ||
      keystate[SDL_SCANCODE_DOWN])
  {
    // If the player is holding down a directional key, accumulate the time
    // since the last tick, and then apply the directional key as many times as
    // needed. The input lag class automatically handles the delays necessary
    // for key repeat.
    inputLag_.accumulate(dt);

    while (inputLag_.step())
    {
      if (mapFocus_)
      {
        // If we are focused on the map, move the map cursor.
        if (keystate[SDL_SCANCODE_LEFT] && cursor_.x() > 0)
        {
          cursor_.x()--;
        }

        if (keystate[SDL_SCANCODE_UP] && cursor_.y() > 0)
        {
          cursor_.y()--;
        }

        if (keystate[SDL_SCANCODE_RIGHT] &&
            cursor_.x() < level_.getSize().w() - 1)
        {
          cursor_.x()++;
        }

        if (keystate[SDL_SCANCODE_DOWN] &&
            cursor_.y() < level_.getSize().h() - 1)
        {
          cursor_.y()++;
        }
      } else {
        // If we are focused on the tileset, rotate through the selection of
        // tiles.
        if (keystate[SDL_SCANCODE_UP])
        {
          selectedTile_--;
        } else if (keystate[SDL_SCANCODE_DOWN])
        {
          selectedTile_++;
        }

        selectedTile_ %= level_.getTileset(layer_).getNumTiles();
      }
    }
  } else if (inputLag_.isActive())
  {
    // If the player is not holding down a directional key, reset the input lag.
    inputLag_.reset();
  }

  // Check for keypress changes since the last tick.
  input_.tick();

  if (input_.wasPressed(SDL_SCANCODE_Z))
  {
    switch (layer_)
    {
      case Layer::map:
      {
        layer_ = Layer::object;

        break;
      }

      case Layer::track:
      {
        layer_ = Layer::map;

        break;
      }

      case Layer::object:
      {
        layer_ = Layer::track;

        break;
      }
    }

    selectedTile_ = 0;

    // Reset the cached map so that it gets redrawn with the focused layer.
    renderedMap_.reset();
  } else if (input_.wasPressed(SDL_SCANCODE_X))
  {
    switch (layer_)
    {
      case Layer::map:
      {
        layer_ = Layer::track;

        break;
      }

      case Layer::track:
      {
        layer_ = Layer::object;

        break;
      }

      case Layer::object:
      {
        layer_ = Layer::map;

        break;
      }
    }

    selectedTile_ = 0;

    // Reset the cached map so that it gets redrawn with the focused layer.
    renderedMap_.reset();
  } else if (input_.wasPressed(SDL_SCANCODE_TAB))
  {
    mapFocus_ = !mapFocus_;
  }

  bool newlyTabbed = false;

  if (input_.wasClicked(SDL_BUTTON_LEFT))
  {
    if (EDITOR_MAP_AREA.contains(input_.getMousePos()))
    {
      if (!mapFocus_)
      {
        newlyTabbed = true;
      }

      mapFocus_ = true;
    } else if (EDITOR_TILESET_AREA.contains(input_.getMousePos()))
    {
      if (mapFocus_)
      {
        newlyTabbed = true;
      }

      mapFocus_ = false;

      selectedTile_ =
        (input_.getMousePos() - EDITOR_TILESET_AREA.pos).y() / TILE_SIZE.h();
    }
  }

  if (mapFocus_)
  {
    if (EDITOR_MAP_AREA.contains(input_.getMousePos()) &&
        ( input_.hasMouseMoved() || newlyTabbed ))
    {
      cursor_ = (input_.getMousePos() - EDITOR_MAP_AREA.pos) / TILE_SIZE;
    }

    if (keystate[SDL_SCANCODE_RETURN] ||
        keystate[SDL_SCANCODE_SPACE] ||
        ( input_.isClicked(SDL_BUTTON_LEFT) &&
          EDITOR_MAP_AREA.contains(input_.getMousePos()) &&
          !newlyTabbed ))
    {
      level_.at(cursor_.x(), cursor_.y(), layer_) = selectedTile_;

      // Reset the cached map so that it gets redrawn with the new tile.
      renderedMap_.reset();
    }
  }
}

void Editor::render(SDL_Renderer* ren)
{
  // Tile the background art.
  for (int y = 0; y <= WINDOW_SIZE.h() / bgSize_.h(); y++)
  {
    for (int x = 0; x <= WINDOW_SIZE.w() / bgSize_.w(); x++)
    {
      SDL_Rect rect {
        x * bgSize_.w(),
        y * bgSize_.h(),
        bgSize_.w(),
        bgSize_.h()
      };

      SDL_RenderCopy(ren, background_.get(), nullptr, &rect);
    }
  }

  // Render and cache the map if the cache is currently empty.
  if (!renderedMap_)
  {
    renderedMap_.reset(
      SDL_CreateTexture(
        ren,
        SDL_PIXELFORMAT_RGBA8888,
        SDL_TEXTUREACCESS_TARGET,
        LEVEL_SIZE.w(),
        LEVEL_SIZE.h()));

    // Render each layer separately, then compose them atop each other. Layers
    // above the currently selected one are drawn semi-transparently.
    texture_ptr mapRender = level_.render(ren, Layer::map);
    texture_ptr trackRender = level_.render(ren, Layer::track);
    texture_ptr objectRender = level_.render(ren, Layer::object);

    if (layer_ < Layer::track)
    {
      SDL_SetTextureBlendMode(trackRender.get(), SDL_BLENDMODE_BLEND);
      SDL_SetTextureAlphaMod(trackRender.get(), 127);
    }

    if (layer_ < Layer::object)
    {
      SDL_SetTextureBlendMode(objectRender.get(), SDL_BLENDMODE_BLEND);
      SDL_SetTextureAlphaMod(objectRender.get(), 127);
    }

    SDL_SetRenderTarget(ren, renderedMap_.get());
    SDL_RenderCopy(ren, mapRender.get(), nullptr, nullptr);
    SDL_RenderCopy(ren, trackRender.get(), nullptr, nullptr);
    SDL_RenderCopy(ren, objectRender.get(), nullptr, nullptr);
    SDL_SetRenderTarget(ren, nullptr);
  }

  // Copy the rendered map onto the editor.
  SDL_Rect mapLoc {
    static_cast<int>(BORDER_SIZE.w()),
    static_cast<int>(BORDER_SIZE.h()),
    static_cast<int>(LEVEL_SIZE.w()),
    static_cast<int>(LEVEL_SIZE.h())
  };

  SDL_RenderCopy(ren, renderedMap_.get(), nullptr, &mapLoc);

  // If the map is focused, draw an outline around it.
  if (mapFocus_)
  {
    SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
    SDL_RenderDrawRect(ren, &mapLoc);
  }

  // Draw a box indicating the position of the map cursor.
  vec2s cursorLoc = BORDER_SIZE + TILE_SIZE * cursor_;

  SDL_Rect cursorRect {
    static_cast<int>(cursorLoc.x()),
    static_cast<int>(cursorLoc.y()),
    static_cast<int>(TILE_SIZE.w()),
    static_cast<int>(TILE_SIZE.h())
  };

  SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
  SDL_RenderDrawRect(ren, &cursorRect);

  // Draw the tileset for the currently selected layer.
  SDL_Rect tilesetLoc {
    static_cast<int>(BORDER_SIZE.w() * 2 + LEVEL_SIZE.w()),
    static_cast<int>(BORDER_SIZE.h()),
    static_cast<int>(TILE_SIZE.w()),
    level_.getTileset(layer_).getSize().h()
  };

  SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
  SDL_RenderFillRect(ren, &tilesetLoc);

  SDL_RenderCopy(
    ren,
    level_.getTileset(layer_).getImage().get(),
    nullptr,
    &tilesetLoc);

  // If the tileset is focused, draw an outline around it.
  if (!mapFocus_)
  {
    SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
    SDL_RenderDrawRect(ren, &tilesetLoc);
  }

  // Draw a box indicating which tile is currently selected.
  SDL_Rect tileCursorRect {
    tilesetLoc.x,
    static_cast<int>((1 + selectedTile_) * TILE_SIZE.h()),
    static_cast<int>(TILE_SIZE.w()),
    static_cast<int>(TILE_SIZE.h())
  };

  SDL_SetRenderDrawColor(ren, 255, 255, 0, 255);
  SDL_RenderDrawRect(ren, &tileCursorRect);
}