From 5089bc373e9ad470aa96d1ad23f7833905dae5ed Mon Sep 17 00:00:00 2001 From: AllyTally Date: Mon, 22 May 2023 14:24:36 -0300 Subject: [PATCH] Add more draw functions, make less code use rectangles Some code still used rectangles to draw things like lines and pixels, so this commit adds more draw functions to support drawing lines and pixels without directly using the renderer. Aside from making generated minimaps draw points instead of 1x1 rectangles, this commit also batches the point drawing for an additional speed increase. --- desktop_version/src/CustomLevels.cpp | 39 +++-- desktop_version/src/Graphics.cpp | 209 +++++++++++++++++++++++---- desktop_version/src/Graphics.h | 34 ++++- 3 files changed, 233 insertions(+), 49 deletions(-) diff --git a/desktop_version/src/CustomLevels.cpp b/desktop_version/src/CustomLevels.cpp index 2dc5300b..29cd286f 100644 --- a/desktop_version/src/CustomLevels.cpp +++ b/desktop_version/src/CustomLevels.cpp @@ -1697,15 +1697,10 @@ void customlevelclass::generatecustomminimap(void) { for (int i2 = 0; i2 < mapwidth; i2++) { - int tm; - if (getroomprop(i2, j2)->tileset == 1) - { - tm = 96; - } - else - { - tm = 196; - } + std::vector dark_points; + std::vector light_points; + + bool dark = getroomprop(i2, j2)->tileset == 1; // Ok, now scan over each square for (int j = 0; j < 9 * map.customzoom; j++) @@ -1737,16 +1732,28 @@ void customlevelclass::generatecustomminimap(void) if (tile >= 1) { - // Fill in this pixel - graphics.fill_rect( - (i2 * 12 * map.customzoom) + i, - (j2 * 9 * map.customzoom) + j, - 1, 1, - graphics.getRGB(tm, tm, tm) - ); + // Add this pixel + SDL_Point point = { (i2 * 12 * map.customzoom) + i, (j2 * 9 * map.customzoom) + j }; + if (dark) + { + dark_points.push_back(point); + } + else + { + light_points.push_back(point); + } } } } + // Draw them all at once + if (!dark_points.empty()) + { + graphics.draw_points(dark_points.data(), dark_points.size(), 96, 96, 96); + } + if (!light_points.empty()) + { + graphics.draw_points(light_points.data(), light_points.size(), 196, 196, 196); + } } } diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 14e9f4bf..a49b4705 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -531,10 +531,15 @@ int Graphics::set_color(const SDL_Color color) return set_color(color.r, color.g, color.b, color.a); } -int Graphics::fill_rect(const SDL_Rect* rect, const int r, const int g, const int b, const int a) +SDL_Color Graphics::get_color(void) { - set_color(r, g, b, a); + SDL_Color color = {0, 0, 0, 0}; + SDL_GetRenderDrawColor(gameScreen.m_renderer, &color.r, &color.g, &color.b, &color.a); + return color; +} +int Graphics::fill_rect(const SDL_Rect* rect) +{ const int result = SDL_RenderFillRect(gameScreen.m_renderer, rect); if (result != 0) { @@ -543,9 +548,21 @@ int Graphics::fill_rect(const SDL_Rect* rect, const int r, const int g, const in return result; } -int Graphics::fill_rect(const int r, const int g, const int b, const int a) +int Graphics::fill_rect(const int x, const int y, const int w, const int h) { - return fill_rect(NULL, r, g, b, a); + SDL_Rect rect = {x, y, w, h}; + return fill_rect(&rect); +} + +int Graphics::fill_rect(void) +{ + return fill_rect(NULL); +} + +int Graphics::fill_rect(const SDL_Rect* rect, const int r, const int g, const int b, const int a) +{ + set_color(r, g, b, a); + return fill_rect(rect); } int Graphics::fill_rect(const SDL_Rect* rect, const int r, const int g, const int b) @@ -584,10 +601,8 @@ int Graphics::fill_rect(const int x, const int y, const int w, const int h, cons return fill_rect(x, y, w, h, color.r, color.g, color.b, color.a); } -int Graphics::draw_rect(const SDL_Rect* rect, const int r, const int g, const int b, const int a) +int Graphics::draw_rect(const SDL_Rect* rect) { - set_color(r, g, b, a); - const int result = SDL_RenderDrawRect(gameScreen.m_renderer, rect); if (result != 0) { @@ -596,6 +611,23 @@ int Graphics::draw_rect(const SDL_Rect* rect, const int r, const int g, const in return result; } +int Graphics::draw_rect(const int x, const int y, const int w, const int h) +{ + SDL_Rect rect{ x, y, w, h }; + return draw_rect(&rect); +} + +int Graphics::draw_rect() +{ + return draw_rect(NULL); +} + +int Graphics::draw_rect(const SDL_Rect* rect, const int r, const int g, const int b, const int a) +{ + set_color(r, g, b, a); + return draw_rect(rect); +} + int Graphics::draw_rect(const SDL_Rect* rect, const int r, const int g, const int b) { return draw_rect(rect, r, g, b, 255); @@ -622,6 +654,127 @@ int Graphics::draw_rect(const int x, const int y, const int w, const int h, cons return draw_rect(x, y, w, h, color.r, color.g, color.b, color.a); } +int Graphics::draw_line(const int x, const int y, const int x2, const int y2) +{ + const int result = SDL_RenderDrawLine(gameScreen.m_renderer, x, y, x2, y2); + if (result != 0) + { + WHINE_ONCE_ARGS(("Could not draw line: %s", SDL_GetError())); + } + return result; +} + +int Graphics::draw_line(const SDL_Rect* rect) +{ + return draw_line(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); +} + +int Graphics::draw_line(const int x, const int y, const int x2, const int y2, const SDL_Color color) +{ + set_color(color); + return draw_line(x, y, x2, y2); +} + +int Graphics::draw_line(const SDL_Rect* rect, const SDL_Color color) +{ + return draw_line(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h, color); +} + +int Graphics::draw_line(const int x, const int y, const int x2, const int y2, const int r, const int g, const int b, const int a) +{ + set_color(r, g, b, a); + return draw_line(x, y, x2, y2); +} + +int Graphics::draw_line(const int x, const int y, const int x2, const int y2, const int r, const int g, const int b) +{ + return draw_line(x, y, x2, y2, r, g, b, 255); +} + +int Graphics::draw_line(const SDL_Rect* rect, const int r, const int g, const int b, const int a) +{ + return draw_line(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h, r, g, b, a); +} + +int Graphics::draw_line(const SDL_Rect* rect, const int r, const int g, const int b) +{ + return draw_line(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h, r, g, b, 255); +} + +int Graphics::draw_point(const int x, const int y) +{ + const int result = SDL_RenderDrawPoint(gameScreen.m_renderer, x, y); + if (result != 0) + { + WHINE_ONCE_ARGS(("Could not draw point: %s", SDL_GetError())); + } + return result; +} + +int Graphics::draw_point(const SDL_Point* point) +{ + return draw_point(point->x, point->y); +} + +int Graphics::draw_point(const int x, const int y, const SDL_Color color) +{ + set_color(color); + return draw_point(x, y); +} + +int Graphics::draw_point(const SDL_Point* point, const SDL_Color color) +{ + return draw_point(point->x, point->y, color); +} + +int Graphics::draw_point(const int x, const int y, const int r, const int g, const int b, const int a) +{ + set_color(r, g, b, a); + return draw_point(x, y); +} + +int Graphics::draw_point(const int x, const int y, const int r, const int g, const int b) +{ + return draw_point(x, y, r, g, b, 255); +} + +int Graphics::draw_point(const SDL_Point* point, const int r, const int g, const int b, const int a) +{ + return draw_point(point->x, point->y, r, g, b, a); +} + +int Graphics::draw_point(const SDL_Point* point, const int r, const int g, const int b) +{ + return draw_point(point->x, point->y, r, g, b, 255); +} + +int Graphics::draw_points(const SDL_Point* points, const int count) +{ + const int result = SDL_RenderDrawPoints(gameScreen.m_renderer, points, count); + if (result != 0) + { + WHINE_ONCE_ARGS(("Could not draw points: %s", SDL_GetError())); + } + return result; +} + +int Graphics::draw_points(const SDL_Point* points, const int count, const SDL_Color color) +{ + set_color(color); + return draw_points(points, count); +} + +int Graphics::draw_points(const SDL_Point* points, const int count, const int r, const int g, const int b, const int a) +{ + set_color(r, g, b, a); + return draw_points(points, count); +} + +int Graphics::draw_points(const SDL_Point* points, const int count, const int r, const int g, const int b) +{ + return draw_points(points, count, r, g, b, 255); +} + void Graphics::draw_sprite(const int x, const int y, const int t, const int r, const int g, const int b) { draw_grid_tile(grphx.im_sprites, t, x, y, sprites_rect.w, sprites_rect.h, r, g, b); @@ -1660,7 +1813,7 @@ bool Graphics::Hitest(SDL_Surface* surface1, SDL_Point p1, SDL_Surface* surface2 } -void Graphics::drawgravityline( int t ) +void Graphics::drawgravityline(const int t, const int x, const int y, const int w, const int h) { if (!INBOUNDS_VEC(t, obj.entities)) { @@ -1672,48 +1825,50 @@ void Graphics::drawgravityline( int t ) { if (game.noflashingmode) { - fill_rect(&line_rect, getRGB(200 - 20, 200 - 20, 200 - 20)); + set_color(200 - 20, 200 - 20, 200 - 20); return; } switch(linestate) { case 0: - fill_rect(&line_rect, getRGB(200-20, 200-20, 200-20)); + set_color(200-20, 200-20, 200-20); break; case 1: - fill_rect(&line_rect, getRGB(245-30, 245-30, 225-30)); + set_color(245-30, 245-30, 225-30); break; case 2: - fill_rect(&line_rect, getRGB(225-30, 245-30, 245-30)); + set_color(225-30, 245-30, 245-30); break; case 3: - fill_rect(&line_rect, getRGB(200-20, 200-20, 164-10)); + set_color(200-20, 200-20, 164-10); break; case 4: - fill_rect(&line_rect, getRGB(196-20, 255-30, 224-20)); + set_color(196-20, 255-30, 224-20); break; case 5: - fill_rect(&line_rect, getRGB(196-20, 235-30, 205-20)); + set_color(196-20, 235-30, 205-20); break; case 6: - fill_rect(&line_rect, getRGB(164-10, 164-10, 164-10)); + set_color(164-10, 164-10, 164-10); break; case 7: - fill_rect(&line_rect, getRGB(205-20, 245-30, 225-30)); + set_color(205-20, 245-30, 225-30); break; case 8: - fill_rect(&line_rect, getRGB(225-30, 255-30, 205-20)); + set_color(225-30, 255-30, 205-20); break; case 9: - fill_rect(&line_rect, getRGB(245-30, 245-30, 245-30)); + set_color(245-30, 245-30, 245-30); break; } } else { - fill_rect(&line_rect, getRGB(96, 96, 96)); + set_color(96, 96, 96); } + + draw_line(x, y, x + w, y + h); } void Graphics::drawtrophytext(void) @@ -2014,19 +2169,11 @@ void Graphics::drawentity(const int i, const int yoff) { oldw -= 24; } - line_rect.x = xp; - line_rect.y = yp - yoff; - line_rect.w = lerp(oldw, obj.entities[i].w); - line_rect.h = 1; - drawgravityline(i); + drawgravityline(i, xp, yp - yoff, lerp(oldw, obj.entities[i].w) - 1, 0); break; } case 6: //Vertical Line - line_rect.x = xp; - line_rect.y = yp - yoff; - line_rect.w = 1; - line_rect.h = obj.entities[i].h; - drawgravityline(i); + drawgravityline(i, xp, yp - yoff, 0, obj.entities[i].h - 1); break; case 7: //Teleporter drawtele(xp, yp - yoff, obj.entities[i].drawframe, obj.entities[i].realcol); @@ -3215,7 +3362,7 @@ void Graphics::flashlight(void) { set_blendmode(SDL_BLENDMODE_NONE); - fill_rect(0xBB, 0xBB, 0xBB, 0xBB); + fill_rect(NULL, 0xBB, 0xBB, 0xBB, 0xBB); } void Graphics::screenshake(void) diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index 6608c64f..35aa4071 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -54,7 +54,7 @@ public: SDL_Color huetilegetcol(); SDL_Color bigchunkygetcol(int t); - void drawgravityline(int t); + void drawgravityline(int t, int x, int y, int w, int h); void drawcoloredtile(int x, int y, int t, int r, int g, int b); @@ -203,16 +203,23 @@ public: int set_color(Uint8 r, Uint8 g, Uint8 b); int set_color(SDL_Color color); + SDL_Color get_color(void); + + int fill_rect(const SDL_Rect* rect); + int fill_rect(int x, int y, int w, int h); + int fill_rect(void); int fill_rect(const SDL_Rect* rect, int r, int g, int b, int a); int fill_rect(int x, int y, int w, int h, int r, int g, int b, int a); int fill_rect(int x, int y, int w, int h, int r, int g, int b); - int fill_rect(int r, int g, int b, int a); int fill_rect(const SDL_Rect* rect, int r, int g, int b); int fill_rect(int r, int g, int b); int fill_rect(const SDL_Rect* rect, SDL_Color color); int fill_rect(int x, int y, int w, int h, SDL_Color color); int fill_rect(SDL_Color color); + int draw_rect(const SDL_Rect* rect); + int draw_rect(int x, int y, int w, int h); + int draw_rect(void); int draw_rect(const SDL_Rect* rect, int r, int g, int b, int a); int draw_rect(int x, int y, int w, int h, int r, int g, int b, int a); int draw_rect(int x, int y, int w, int h, int r, int g, int b); @@ -220,6 +227,29 @@ public: int draw_rect(const SDL_Rect* rect, SDL_Color color); int draw_rect(int x, int y, int w, int h, SDL_Color color); + int draw_line(int x, int y, int x2, int y2); + int draw_line(const SDL_Rect* rect); + int draw_line(int x, int y, int x2, int y2, SDL_Color color); + int draw_line(const SDL_Rect* rect, SDL_Color color); + int draw_line(int x, int y, int x2, int y2, int r, int g, int b, int a); + int draw_line(int x, int y, int x2, int y2, int r, int g, int b); + int draw_line(const SDL_Rect* rect, int r, int g, int b, int a); + int draw_line(const SDL_Rect* rect, int r, int g, int b); + + int draw_point(int x, int y); + int draw_point(const SDL_Point* point); + int draw_point(int x, int y, SDL_Color color); + int draw_point(const SDL_Point* point, SDL_Color color); + int draw_point(int x, int y, int r, int g, int b, int a); + int draw_point(int x, int y, int r, int g, int b); + int draw_point(const SDL_Point* point, int r, int g, int b, int a); + int draw_point(const SDL_Point* point, int r, int g, int b); + + int draw_points(const SDL_Point* points, int count); + int draw_points(const SDL_Point* points, int count, SDL_Color color); + int draw_points(const SDL_Point* points, int count, int r, int g, int b, int a); + int draw_points(const SDL_Point* points, int count, int r, int g, int b); + void map_tab(int opt, const char* text, bool selected = false); void map_option(int opt, int num_opts, const std::string& text, bool selected = false);