summary refs log tree commit diff stats
path: root/src/renderer.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2022-03-14 14:11:05 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2022-03-14 14:11:05 -0400
commit7cd515fcfa1a5aac5605fcc63381033563fbf5d7 (patch)
tree2a375db2fe6c1476a111c1f3e83e50fd2b68124e /src/renderer.cpp
parent042da1570181dc80ee76860582f6a3c97641c14a (diff)
downloadether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.tar.gz
ether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.tar.bz2
ether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.zip
map is now "infinite" using chunks that dynamically load like in diamond&pearl
game is also slow as shit now
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r--src/renderer.cpp83
1 files changed, 53 insertions, 30 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index 9ef8bb9..1f51e0a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -12,7 +12,7 @@ Renderer::Renderer()
12 GAME_HEIGHT, 12 GAME_HEIGHT,
13 SDL_WINDOW_SHOWN)); 13 SDL_WINDOW_SHOWN));
14 14
15 SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); 15 //SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP);
16 16
17 if (!win_) 17 if (!win_)
18 { 18 {
@@ -115,13 +115,16 @@ void Renderer::renderGame(
115 const Game& game, 115 const Game& game,
116 bool drawDark) 116 bool drawDark)
117{ 117{
118 int windowTileWidth = game.curZoom * ZOOM_X_FACTOR + 2;
119 int windowTileHeight = game.curZoom * ZOOM_Y_FACTOR + 2;
120
118 texture_ptr canvas( 121 texture_ptr canvas(
119 SDL_CreateTexture( 122 SDL_CreateTexture(
120 ren_.get(), 123 ren_.get(),
121 SDL_PIXELFORMAT_RGBA8888, 124 SDL_PIXELFORMAT_RGBA8888,
122 SDL_TEXTUREACCESS_TARGET, 125 SDL_TEXTUREACCESS_TARGET,
123 TILE_WIDTH * game.map.getWidth(), 126 windowTileWidth * TILE_WIDTH,
124 TILE_HEIGHT * game.map.getHeight())); 127 windowTileHeight * TILE_HEIGHT));
125 128
126 if (!canvas) 129 if (!canvas)
127 { 130 {
@@ -133,15 +136,17 @@ void Renderer::renderGame(
133 SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255); 136 SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255);
134 SDL_RenderClear(ren_.get()); 137 SDL_RenderClear(ren_.get());
135 138
136 for (int y = game.map.getTop(); y < game.map.getBottom(); y++) 139 int leftmost = game.player_x - game.curZoom * ZOOM_X_FACTOR / 2 - 1;
140 int topmost = game.player_y - game.curZoom * ZOOM_Y_FACTOR / 2 - 1;
141 for (int y = topmost; y < topmost + windowTileHeight; y++)
137 { 142 {
138 for (int x = game.map.getLeft(); x < game.map.getRight(); x++) 143 for (int x = leftmost; x < leftmost + windowTileWidth; x++)
139 { 144 {
140 bool draw = true; 145 bool draw = true;
141 bool drawColour = false; 146 bool drawColour = false;
142 SDL_Rect rect { 147 SDL_Rect rect {
143 game.map.getTrueX(x) * TILE_WIDTH, 148 (x - leftmost) * TILE_WIDTH,
144 game.map.getTrueY(y) * TILE_HEIGHT, 149 (y - topmost) * TILE_HEIGHT,
145 TILE_WIDTH, 150 TILE_WIDTH,
146 TILE_HEIGHT}; 151 TILE_HEIGHT};
147 152
@@ -190,14 +195,14 @@ void Renderer::renderGame(
190 195
191 if (game.renderPlayer) { 196 if (game.renderPlayer) {
192 SDL_Rect rect { 197 SDL_Rect rect {
193 game.map.getTrueX(game.player_x) * TILE_WIDTH, 198 (game.player_x - leftmost) * TILE_WIDTH,
194 game.map.getTrueY(game.player_y) * TILE_HEIGHT, 199 (game.player_y - topmost) * TILE_HEIGHT,
195 TILE_WIDTH, 200 TILE_WIDTH,
196 TILE_HEIGHT}; 201 TILE_HEIGHT};
197 202
198 if (game.moving) { 203 if (game.moving) {
199 rect.x = game.moveProgress.getProgress(game.map.getTrueX(game.player_oldx) * TILE_WIDTH, rect.x); 204 rect.x = game.moveProgress.getProgress((game.player_oldx - leftmost) * TILE_WIDTH, rect.x);
200 rect.y = game.moveProgress.getProgress(game.map.getTrueY(game.player_oldy) * TILE_HEIGHT, rect.y); 205 rect.y = game.moveProgress.getProgress((game.player_oldy - topmost) * TILE_HEIGHT, rect.y);
201 } 206 }
202 207
203 SDL_RenderCopy(ren_.get(), playerSheet_.get(), &game.playerAnim.getRenderRect(), &rect); 208 SDL_RenderCopy(ren_.get(), playerSheet_.get(), &game.playerAnim.getRenderRect(), &rect);
@@ -208,8 +213,8 @@ void Renderer::renderGame(
208 ren_.get(), 213 ren_.get(),
209 SDL_PIXELFORMAT_RGBA8888, 214 SDL_PIXELFORMAT_RGBA8888,
210 SDL_TEXTUREACCESS_TARGET, 215 SDL_TEXTUREACCESS_TARGET,
211 TILE_WIDTH * game.map.getWidth(), 216 windowTileWidth * TILE_WIDTH,
212 TILE_HEIGHT * game.map.getHeight())); 217 windowTileHeight * TILE_HEIGHT));
213 218
214 if (!mask) 219 if (!mask)
215 { 220 {
@@ -220,9 +225,9 @@ void Renderer::renderGame(
220 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); 225 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
221 SDL_RenderClear(ren_.get()); 226 SDL_RenderClear(ren_.get());
222 227
223 for (int y = game.map.getTop(); y < game.map.getBottom(); y++) 228 for (int y = topmost; y < topmost + windowTileHeight; y++)
224 { 229 {
225 for (int x = game.map.getLeft(); x < game.map.getRight(); x++) 230 for (int x = leftmost; x < leftmost + windowTileWidth; x++)
226 { 231 {
227 if (game.map.at(x,y).lightType != Source::None) 232 if (game.map.at(x,y).lightType != Source::None)
228 { 233 {
@@ -231,8 +236,8 @@ void Renderer::renderGame(
231 ren_.get(), 236 ren_.get(),
232 SDL_PIXELFORMAT_RGBA8888, 237 SDL_PIXELFORMAT_RGBA8888,
233 SDL_TEXTUREACCESS_TARGET, 238 SDL_TEXTUREACCESS_TARGET,
234 TILE_WIDTH * game.map.getWidth(), 239 windowTileWidth * TILE_WIDTH,
235 TILE_HEIGHT * game.map.getHeight())); 240 windowTileHeight * TILE_HEIGHT));
236 241
237 if (!sourceMask) 242 if (!sourceMask)
238 { 243 {
@@ -244,11 +249,11 @@ void Renderer::renderGame(
244 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); 249 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
245 SDL_RenderClear(ren_.get()); 250 SDL_RenderClear(ren_.get());
246 251
247 int posToUseX = game.map.getTrueX(x); 252 int posToUseX = x - leftmost;
248 int posToUseY = game.map.getTrueY(y); 253 int posToUseY = y - topmost;
249 if (game.map.at(x,y).lightType == Source::Player && game.moving) { 254 if (game.map.at(x,y).lightType == Source::Player && game.moving) {
250 posToUseX = game.moveProgress.getProgress(game.map.getTrueX(game.player_oldx), posToUseX); 255 posToUseX = game.moveProgress.getProgress((game.player_oldx) - leftmost, posToUseX);
251 posToUseY = game.moveProgress.getProgress(game.map.getTrueY(game.player_oldy), posToUseY); 256 posToUseY = game.moveProgress.getProgress((game.player_oldy) - topmost, posToUseY);
252 } 257 }
253 258
254 int fadeX = posToUseX - game.map.at(x,y).lightRadius; 259 int fadeX = posToUseX - game.map.at(x,y).lightRadius;
@@ -283,8 +288,8 @@ void Renderer::renderGame(
283 if (!game.map.at(x,y).litTiles.count({sx, sy})) 288 if (!game.map.at(x,y).litTiles.count({sx, sy}))
284 { 289 {
285 SDL_Rect rect { 290 SDL_Rect rect {
286 game.map.getTrueX(sx) * TILE_WIDTH, 291 (sx - leftmost) * TILE_WIDTH,
287 game.map.getTrueY(sy) * TILE_HEIGHT, 292 (sy - topmost) * TILE_HEIGHT,
288 TILE_WIDTH, 293 TILE_WIDTH,
289 TILE_HEIGHT}; 294 TILE_HEIGHT};
290 295
@@ -306,10 +311,28 @@ void Renderer::renderGame(
306 311
307 SDL_SetRenderTarget(ren_.get(), nullptr); 312 SDL_SetRenderTarget(ren_.get(), nullptr);
308 313
309 SDL_Rect zoomRect; 314 // TODO: this is just moving interp. we also need zoom
315 SDL_Rect zoomRect {
316 TILE_WIDTH, TILE_HEIGHT, (windowTileWidth - 2) * TILE_WIDTH, (windowTileHeight - 2) * TILE_HEIGHT
317 };
318
319 if (game.moving) {
320 double interp =
321 static_cast<double>(game.zoomProgress) /
322 static_cast<double>(game.zoomLength);
323
324 if (game.player_x > game.player_oldx) {
325 zoomRect.x = game.moveProgress.getProgress(0, TILE_WIDTH);
326 } else if (game.player_x < game.player_oldx) {
327 zoomRect.x = game.moveProgress.getProgress(2*TILE_WIDTH, TILE_WIDTH);
328 }
310 329
311 std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) = 330 if (game.player_y > game.player_oldy) {
312 calculateZoomRect(game); 331 zoomRect.y = game.moveProgress.getProgress(0, TILE_HEIGHT);
332 } else if (game.player_y < game.player_oldy) {
333 zoomRect.y = game.moveProgress.getProgress(TILE_HEIGHT*2, TILE_HEIGHT);
334 }
335 }
313 336
314 SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); 337 SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr);
315 SDL_RenderPresent(ren_.get()); 338 SDL_RenderPresent(ren_.get());
@@ -358,12 +381,12 @@ void Renderer::renderTitle(int num, double fade) {
358 381
359std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) 382std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game)
360{ 383{
361 int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH;
362 int y = game.map.getTrueY(game.curBoundY) * TILE_HEIGHT;
363 int w = game.curZoom * TILE_WIDTH * ZOOM_X_FACTOR; 384 int w = game.curZoom * TILE_WIDTH * ZOOM_X_FACTOR;
364 int h = game.curZoom * TILE_HEIGHT * ZOOM_Y_FACTOR; 385 int h = game.curZoom * TILE_HEIGHT * ZOOM_Y_FACTOR;
386 int x = (game.map.getTrueX(game.player_x) * TILE_WIDTH) - (w / 2);
387 int y = (game.map.getTrueY(game.player_y) * TILE_HEIGHT) - (h / 2);
365 388
366 if (game.zooming) 389 /*if (game.zooming)
367 { 390 {
368 double interp = 391 double interp =
369 static_cast<double>(game.zoomProgress) / 392 static_cast<double>(game.zoomProgress) /
@@ -373,7 +396,7 @@ std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game)
373 y = (y - game.lastZoomTop) * interp + game.lastZoomTop; 396 y = (y - game.lastZoomTop) * interp + game.lastZoomTop;
374 w = (w - game.lastZoomWidth) * interp + game.lastZoomWidth; 397 w = (w - game.lastZoomWidth) * interp + game.lastZoomWidth;
375 h = (h - game.lastZoomHeight) * interp + game.lastZoomHeight; 398 h = (h - game.lastZoomHeight) * interp + game.lastZoomHeight;
376 } 399 }*/
377 400
378 return {x, y, w, h}; 401 return {x, y, w, h};
379} 402}