Skip to content

Commit

Permalink
Merge branch 'feature/surface-scaling'
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 16, 2014
2 parents b641d01 + db5b2cd commit 1c73821
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 57 deletions.
5 changes: 3 additions & 2 deletions src/math/rectf.hpp
Expand Up @@ -19,6 +19,7 @@

#include <assert.h>

#include "math/sizef.hpp"
#include "math/vector.hpp"
#include "object/anchor_point.hpp"

Expand Down Expand Up @@ -89,9 +90,9 @@ class Rectf
set_width(width);
set_height(height);
}
Vector get_size()
Sizef get_size() const
{
return Vector(get_width(), get_height());
return Sizef(get_width(), get_height());
}

void move(const Vector& v)
Expand Down
12 changes: 12 additions & 0 deletions src/math/sizef.hpp
Expand Up @@ -19,6 +19,8 @@

#include <iosfwd>

#include "math/vector.hpp"

class Size;

class Sizef
Expand All @@ -29,6 +31,11 @@ class Sizef
height(0.0f)
{}

Sizef(const Vector& v) :
width(v.x),
height(v.y)
{}

Sizef(float width_, float height_) :
width(width_),
height(height_)
Expand Down Expand Up @@ -69,6 +76,11 @@ class Sizef
return *this;
}

Vector as_vector() const
{
return Vector(width, height);
}

public:
float width;
float height;
Expand Down
4 changes: 2 additions & 2 deletions src/object/bicycle_platform.cpp
Expand Up @@ -102,7 +102,7 @@ BicyclePlatform::update(float elapsed_time)
angle = master->angle + M_PI;
while (angle < 0) { angle += 2*M_PI; }
while (angle > 2*M_PI) { angle -= 2*M_PI; }
Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size().as_vector() * 0.5);
movement = dest - get_pos();
}
if (this == master) {
Expand All @@ -118,7 +118,7 @@ BicyclePlatform::update(float elapsed_time)
while (angle < 0) { angle += 2*M_PI; }
while (angle > 2*M_PI) { angle -= 2*M_PI; }
angular_speed = std::min(std::max(angular_speed, static_cast<float>(-128*M_PI*elapsed_time)), static_cast<float>(128*M_PI*elapsed_time));
Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size().as_vector() * 0.5);
movement = dest - get_pos();

center += Vector(angular_speed, 0) * elapsed_time * 32;
Expand Down
2 changes: 1 addition & 1 deletion src/object/bonus_block.cpp
Expand Up @@ -474,7 +474,7 @@ BonusBlock::draw(DrawingContext& context){
Block::draw(context);
// then Draw the light if on.
if(sprite->get_action() == "on") {
Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
Vector pos = get_pos() + (Vector(bbox.get_size().as_vector()) - lightsprite->get_size()) / 2;
context.push_target();
context.set_target(DrawingContext::LIGHTMAP);
context.draw_surface(lightsprite, pos, 10);
Expand Down
4 changes: 2 additions & 2 deletions src/object/moving_sprite.cpp
Expand Up @@ -124,10 +124,10 @@ MovingSprite::set_action(const std::string& action, int loops)
void
MovingSprite::set_action_centered(const std::string& action, int loops)
{
Vector old_size = bbox.get_size();
Vector old_size = bbox.get_size().as_vector();
sprite->set_action(action, loops);
set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
set_pos(get_pos() - (bbox.get_size() - old_size) / 2);
set_pos(get_pos() - (Vector(bbox.get_size().as_vector()) - old_size) / 2);
}

void
Expand Down
6 changes: 4 additions & 2 deletions src/sprite/sprite.cpp
Expand Up @@ -152,8 +152,10 @@ Sprite::draw_part(DrawingContext& context, const Vector& source,
assert(action != 0);
update();

context.draw_surface_part(action->surfaces[frameidx], source, size,
pos - Vector(action->x_offset, action->y_offset),
context.draw_surface_part(action->surfaces[frameidx],
Rectf(source, Sizef(size)),
Rectf(pos - Vector(action->x_offset, action->y_offset),
action->surfaces[frameidx]->get_size()),
layer + action->z_order);
}

Expand Down
6 changes: 4 additions & 2 deletions src/supertux/title_screen.cpp
Expand Up @@ -119,8 +119,10 @@ TitleScreen::draw(DrawingContext& context)
Sector* sector = titlesession->get_current_sector();
sector->draw(context);

// FIXME: Add something to scale the frame to the resolution of the screen
//context.draw_surface(frame, Vector(0,0),LAYER_FOREGROUND1);
context.draw_surface_part(frame,
Rectf(0, 0, frame->get_width(), frame->get_height()),
Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
LAYER_FOREGROUND1);

context.draw_text(Resources::small_font,
copyright_text,
Expand Down
26 changes: 6 additions & 20 deletions src/video/drawing_context.cpp
Expand Up @@ -113,40 +113,26 @@ DrawingContext::draw_surface(SurfacePtr surface, const Vector& position,
}

void
DrawingContext::draw_surface_part(SurfacePtr surface, const Vector& source,
const Vector& size, const Vector& dest, int layer)
DrawingContext::draw_surface_part(SurfacePtr surface,
const Rectf& srcrect, const Rectf& dstrect,
int layer)
{
assert(surface != 0);

DrawingRequest* request = new(obst) DrawingRequest();

request->target = target;
request->type = SURFACE_PART;
request->pos = transform.apply(dest);
request->pos = transform.apply(dstrect.p1);
request->layer = layer;
request->drawing_effect = transform.drawing_effect;
request->alpha = transform.alpha;

SurfacePartRequest* surfacepartrequest = new(obst) SurfacePartRequest();
surfacepartrequest->size = size;
surfacepartrequest->source = source;
surfacepartrequest->srcrect = srcrect;
surfacepartrequest->dstsize = dstrect.get_size();
surfacepartrequest->surface = surface.get();

// clip on screen borders
if(request->pos.x < 0) {
surfacepartrequest->size.x += request->pos.x;
if(surfacepartrequest->size.x <= 0)
return;
surfacepartrequest->source.x -= request->pos.x;
request->pos.x = 0;
}
if(request->pos.y < 0) {
surfacepartrequest->size.y += request->pos.y;
if(surfacepartrequest->size.y <= 0)
return;
surfacepartrequest->source.y -= request->pos.y;
request->pos.y = 0;
}
request->request_data = surfacepartrequest;

requests->push_back(request);
Expand Down
5 changes: 3 additions & 2 deletions src/video/drawing_context.hpp
Expand Up @@ -99,8 +99,9 @@ class DrawingContext
float angle, const Color& color, const Blend& blend,
int layer);
/// Adds a drawing request for part of a surface.
void draw_surface_part(SurfacePtr surface, const Vector& source,
const Vector& size, const Vector& dest, int layer);
void draw_surface_part(SurfacePtr surface,
const Rectf& srcrect, const Rectf& dstrect,
int layer);
/// Draws a text.
void draw_text(FontPtr font, const std::string& text,
const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
Expand Down
10 changes: 6 additions & 4 deletions src/video/drawing_request.hpp
Expand Up @@ -23,6 +23,8 @@

#include <stdint.h>

#include "math/rectf.hpp"
#include "math/sizef.hpp"
#include "math/vector.hpp"
#include "video/color.hpp"
#include "video/drawing_context.hpp"
Expand Down Expand Up @@ -59,13 +61,13 @@ struct SurfacePartRequest : public DrawingRequestData
{
SurfacePartRequest() :
surface(),
source(),
size()
srcrect(),
dstsize()
{}

const Surface* surface;
Vector source;
Vector size;
Rectf srcrect;
Sizef dstsize;

private:
SurfacePartRequest(const SurfacePartRequest&) = delete;
Expand Down
4 changes: 2 additions & 2 deletions src/video/font.cpp
Expand Up @@ -434,8 +434,8 @@ Font::draw_chars(Renderer *renderer, bool notshadow, const std::string& text,
request.alpha = alpha;

SurfacePartRequest surfacepartrequest;
surfacepartrequest.size = glyph.rect.p2 - glyph.rect.p1;
surfacepartrequest.source = glyph.rect.p1;
surfacepartrequest.srcrect = glyph.rect;
surfacepartrequest.dstsize = glyph.rect.get_size();
surfacepartrequest.surface = notshadow ? glyph_surfaces[glyph.surface_idx].get() : shadow_surfaces[glyph.surface_idx].get();

request.request_data = &surfacepartrequest;
Expand Down
12 changes: 6 additions & 6 deletions src/video/gl/gl_lightmap.cpp
Expand Up @@ -179,15 +179,15 @@ GLLightmap::draw_surface_part(const DrawingRequest& request)
float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();

float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();

glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
intern_draw(request.pos.x, request.pos.y,
request.pos.x + surfacepartrequest->size.x,
request.pos.y + surfacepartrequest->size.y,
request.pos.x + surfacepartrequest->dstsize.width,
request.pos.y + surfacepartrequest->dstsize.height,
uv_left,
uv_top,
uv_right,
Expand Down
12 changes: 6 additions & 6 deletions src/video/gl/gl_renderer.cpp
Expand Up @@ -158,19 +158,19 @@ GLRenderer::draw_surface_part(const DrawingRequest& request)
float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();

float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();

GLuint th = gltexture->get_handle();
if (th != last_texture) {
last_texture = th;
glBindTexture(GL_TEXTURE_2D, th);
}
intern_draw(request.pos.x, request.pos.y,
request.pos.x + surfacepartrequest->size.x,
request.pos.y + surfacepartrequest->size.y,
request.pos.x + surfacepartrequest->dstsize.width,
request.pos.y + surfacepartrequest->dstsize.height,
uv_left,
uv_top,
uv_right,
Expand Down
12 changes: 6 additions & 6 deletions src/video/sdl/sdl_painter.cpp
Expand Up @@ -99,16 +99,16 @@ SDLPainter::draw_surface_part(SDL_Renderer* renderer, const DrawingRequest& requ
boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());

SDL_Rect src_rect;
src_rect.x = surfacepartrequest->source.x;
src_rect.y = surfacepartrequest->source.y;
src_rect.w = surfacepartrequest->size.x;
src_rect.h = surfacepartrequest->size.y;
src_rect.x = surfacepartrequest->srcrect.p1.x;
src_rect.y = surfacepartrequest->srcrect.p1.y;
src_rect.w = surfacepartrequest->srcrect.get_width();
src_rect.h = surfacepartrequest->srcrect.get_height();

SDL_Rect dst_rect;
dst_rect.x = request.pos.x;
dst_rect.y = request.pos.y;
dst_rect.w = surfacepartrequest->size.x;
dst_rect.h = surfacepartrequest->size.y;
dst_rect.w = surfacepartrequest->dstsize.width;
dst_rect.h = surfacepartrequest->dstsize.height;

Uint8 r = static_cast<Uint8>(request.color.red * 255);
Uint8 g = static_cast<Uint8>(request.color.green * 255);
Expand Down

0 comments on commit 1c73821

Please sign in to comment.