Skip to content

Commit

Permalink
Port the load screen to use SDL_gpu.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipk committed Jun 27, 2014
1 parent 49e68b0 commit ebd6f18
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 64 deletions.
147 changes: 84 additions & 63 deletions src/loadscreen.cpp
Expand Up @@ -27,10 +27,7 @@
#include "sdl/rect.hpp"
#include "video.hpp"
#include "image.hpp"

#if SDL_VERSION_ATLEAST(2,0,0)
#include "text.hpp"
#endif

#include <SDL_events.h>
#include <SDL_image.h>
Expand Down Expand Up @@ -77,7 +74,7 @@ loadscreen::loadscreen(CVideo &screen, const int percent):
#if SDL_VERSION_ATLEAST(2,0,0)
logo_texture_(image::get_texture("misc/logo.png")),
#else
logo_surface_(image::get_image("misc/logo.png")),
logo_image_(NULL),
#endif
logo_drawn_(false),
pby_offset_(0),
Expand All @@ -88,14 +85,18 @@ loadscreen::loadscreen(CVideo &screen, const int percent):
ERR_DP << "loadscreen: Failed to load the logo" << std::endl;
}
#else
if (logo_surface_.null()) {
surface surf = image::get_image("misc/logo.png");
logo_image_ = GPU_CopyImageFromSurface(surf);
if (logo_image_ == NULL) {
ERR_DP << "loadscreen: Failed to load the logo" << std::endl;
}
#endif
textarea_.x = textarea_.y = textarea_.w = textarea_.h = 0;
}
void loadscreen::draw_screen(const std::string &text)
{
GPU_Target *screen = get_render_target();

// Set progress bar parameters:
//
// RGB-values for finished piece.
Expand All @@ -108,11 +109,11 @@ void loadscreen::draw_screen(const std::string &text)
int bw = 1;
// Border inner spacing width.
int bispw = 1;
bw = 2*(bw+bispw) > screen_.getx() ? 0: 2*(bw+bispw) > screen_.gety() ? 0: bw;
bw = 2*(bw+bispw) > screen->w ? 0: 2*(bw+bispw) > screen->h ? 0: bw;
// Available width.
int scrx = screen_.getx() - 2*(bw+bispw);
int scrx = screen->w - 2*(bw+bispw);
// Available height.
int scry = screen_.gety() - 2*(bw+bispw);
int scry = screen->h - 2*(bw+bispw);
// Used width.
int pbw = scrx/2;
// Used height.
Expand Down Expand Up @@ -207,8 +208,7 @@ void loadscreen::draw_screen(const std::string &text)
}
CVideo::get_window()->render();
#else
surface gdis = screen_.getSurface();
SDL_Rect area;
int x1, y1, x2, y2;

// Pump events and make sure to redraw the logo if there's a chance that it's been obscured
SDL_Event ev;
Expand All @@ -220,75 +220,103 @@ void loadscreen::draw_screen(const std::string &text)
}

// Draw logo if it was successfully loaded.
if (logo_surface_ && !logo_drawn_) {
area.x = (screen_.getx () - logo_surface_->w) / 2;
area.y = ((scry - logo_surface_->h) / 2) - pbh;
area.w = logo_surface_->w;
area.h = logo_surface_->h;
if (logo_image_ && !logo_drawn_) {
x1 = screen->w / 2;
y1 = ((scry - logo_image_->h) / 2) - pbh;
// Check if we have enough pixels to display it.
if (area.x > 0 && area.y > 0) {
pby_offset_ = (pbh + area.h)/2;
sdl_blit(logo_surface_, 0, gdis, &area);
if (x1 > 0 && y1 > 0) {
pby_offset_ = (pbh + logo_image_->h)/2;
GPU_Blit(logo_image_, NULL, get_render_target(), x1, y1 + logo_image_->h/2);
} else {
if (!screen_.faked()) { // Avoid error if --nogui is used.
ERR_DP << "loadscreen: Logo image is too big." << std::endl;
}
}
logo_drawn_ = true;
update_rect(area.x, area.y, area.w, area.h);
}
int pbx = (scrx - pbw)/2; // Horizontal location.
int pby = (scry - pbh)/2 + pby_offset_; // Vertical location.

SDL_Color border_color;
border_color.r = bcr;
border_color.g = bcg;
border_color.b = bcb;
border_color.unused = 255;

SDL_Color bar_color;
bar_color.r = fcr;
bar_color.g = fcg;
bar_color.b = fcb;
bar_color.unused = 255;

SDL_Color light_color;
light_color.r = lcr;
light_color.g = lcg;
light_color.b = lcb;
light_color.unused = 255;

// Draw top border.
area.x = pbx; area.y = pby;
area.w = pbw + 2*(bw+bispw); area.h = bw;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,bcr,bcg,bcb));
x1 = pbx; y1 = pby;
x2 = pbw + 2*(bw+bispw) + x1; y2 = bw + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, border_color);
// Draw bottom border.
area.x = pbx; area.y = pby + pbh + bw + 2*bispw;
area.w = pbw + 2*(bw+bispw); area.h = bw;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,bcr,bcg,bcb));
x1 = pbx; y1 = pby + pbh + bw + 2*bispw;
x2 = pbw + 2*(bw+bispw) + x1; y2 = bw + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, border_color);
// Draw left border.
area.x = pbx; area.y = pby + bw;
area.w = bw; area.h = pbh + 2*bispw;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,bcr,bcg,bcb));
x1 = pbx; y1 = pby + bw;
x2 = bw + x1; y2 = pbh + 2*bispw + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, border_color);
// Draw right border.
area.x = pbx + pbw + bw + 2*bispw; area.y = pby + bw;
area.w = bw; area.h = pbh + 2*bispw;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,bcr,bcg,bcb));
x1 = pbx + pbw + bw + 2*bispw; y1 = pby + bw;
x2 = bw + x1; y2 = pbh + 2*bispw + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, border_color);
// Draw the finished bar area.
area.x = pbx + bw + bispw; area.y = pby + bw + bispw;
area.w = (prcnt_ * pbw) / 100; area.h = pbh;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,fcr,fcg,fcb));

SDL_Rect lightning = area;
lightning.h = lightning_thickness;
x1 = pbx + bw + bispw; y1 = pby + bw + bispw;
x2 = (prcnt_ * pbw) / 100 + x1; y2 = pbh + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, bar_color);

int yl = y1 + lightning_thickness;
bar_color.r = (fcr*3 + 255)/4;
bar_color.g = (fcg*3 + 255)/4;
bar_color.b = (fcb*3 + 255)/4;
//we add 25% of white to the color of the bar to simulate a light effect
sdl::fill_rect(gdis,&lightning,SDL_MapRGB(gdis->format,(fcr*3+255)/4,(fcg*3+255)/4,(fcb*3+255)/4));
lightning.y = area.y+area.h-lightning.h;
GPU_RectangleFilled(screen, x1, y1, x2, yl, bar_color);
y1 = y1+y2-yl;
//remove 50% of color to simulate a shadow effect
sdl::fill_rect(gdis,&lightning,SDL_MapRGB(gdis->format,fcr/2,fcg/2,fcb/2));
bar_color.r = fcr/2;
bar_color.g = fcg/2;
bar_color.b = fcb/2;
GPU_RectangleFilled(screen, x1, y1, x2, y2, bar_color);

// Draw the leftover bar area.
area.x = pbx + bw + bispw + (prcnt_ * pbw) / 100; area.y = pby + bw + bispw;
area.w = ((100 - prcnt_) * pbw) / 100; area.h = pbh;
sdl::fill_rect(gdis,&area,SDL_MapRGB(gdis->format,lcr,lcg,lcb));

// Clear the last text and draw new if text is provided.
x1 = pbx + bw + bispw + (prcnt_ * pbw) / 100; y1 = pby + bw + bispw;
x2 = ((100 - prcnt_) * pbw) / 100 + x1; y2 = pbh + y1;
GPU_RectangleFilled(screen, x1, y1, x2, y2, light_color);
if (!text.empty())
{
SDL_Rect oldarea = textarea_;
sdl::fill_rect(gdis,&textarea_,SDL_MapRGB(gdis->format,0,0,0));
textarea_ = font::line_size(text, font::SIZE_NORMAL);
textarea_.x = scrx/2 + bw + bispw - textarea_.w / 2;
static const SDL_Color black = { 0, 0, 0, 255 };
const int tx1 = textarea_.x,
ty1 = textarea_.y,
tx2 = tx1 + textarea_.w,
ty2 = ty1 + textarea_.h;

GPU_RectangleFilled(screen, tx1, ty1, tx2, ty2, black);

font::ttext txt;
txt.set_text(text, false);
surface surf = txt.render();
textarea_.h = surf->h;
textarea_.w = surf->w;
textarea_.x = scrx/2 - textarea_.w / 2;
textarea_.y = pby + pbh + 4*(bw + bispw);
textarea_ = font::draw_text(&screen_,textarea_,font::SIZE_NORMAL,font::NORMAL_COLOR,text,textarea_.x,textarea_.y);
SDL_Rect refresh = sdl::union_rects(oldarea, textarea_);
update_rect(refresh.x, refresh.y, refresh.w, refresh.h);

GPU_Image *img = GPU_CopyImageFromSurface(surf);
GPU_Blit(img, NULL, screen, scrx/2, textarea_.y + textarea_.h/2);
GPU_FreeImage(img);
}
// Update the rectangle.
update_rect(pbx, pby, pbw + 2*(bw + bispw), pbh + 2*(bw + bispw));
screen_.flip();

GPU_Flip(get_render_target());
#endif
}

Expand All @@ -297,14 +325,7 @@ void loadscreen::clear_screen()
#if SDL_VERSION_ATLEAST(2,0,0)
CVideo::get_window()->fill(0,0,0);
#else
int scrx = screen_.getx(); // Screen width.
int scry = screen_.gety(); // Screen height.
SDL_Rect area = sdl::create_rect(0, 0, scrx, scry); // Screen area.
surface disp(screen_.getSurface()); // Screen surface.
// Make everything black.
sdl::fill_rect(disp,&area,SDL_MapRGB(disp->format,0,0,0));
update_whole_screen();
screen_.flip();
GPU_Clear(get_render_target());
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/loadscreen.hpp
Expand Up @@ -20,6 +20,7 @@
class CVideo;

#include "sdl/utils.hpp"
#include "sdl/gpu.hpp"

#if SDL_VERSION_ATLEAST(2,0,0)
#include "sdl/texture.hpp"
Expand Down Expand Up @@ -81,7 +82,7 @@ class loadscreen {
#if SDL_VERSION_ATLEAST(2,0,0)
sdl::ttexture logo_texture_;
#else
surface logo_surface_;
GPU_Image *logo_image_;
#endif
bool logo_drawn_;
int pby_offset_;
Expand Down

0 comments on commit ebd6f18

Please sign in to comment.