Browse Source

rendering

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
d06bede011

+ 2
- 1
core.mod/src/entities/EntItemStack.cc View File

@@ -17,7 +17,8 @@ EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF &params):

void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) {
SDL_Rect rect = item_->image_.frameRect();
win.showTexture(body_.pos_, item_->image_.texture_.get(), &rect);
win.showTexture(body_.pos_, item_->image_.texture_.get(), &rect,
{ .hscale = 0.5, .vscale = 0.5 });
}

void EntItemStack::tick(const Swan::Context &ctx, float dt) {

+ 1
- 1
core.mod/src/entities/EntPlayer.cc View File

@@ -11,7 +11,7 @@ EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF &params):
}

void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) {
//body_.outline(win);
body_.outline(win);
anims_[(int)state_].draw(body_.pos_ - Swan::Vec2(0.2, 0.1), win);
}


+ 36
- 10
libswan/include/swan/Win.h View File

@@ -4,6 +4,7 @@
#include "common.h"

#include <SDL2/SDL.h>
#include <optional>

namespace Swan {

@@ -21,22 +22,47 @@ public:
Vec2 getSize() {
int w, h;
SDL_GetWindowSize(window_, &w, &h);
return Vec2((float)w / TILE_SIZE, (float)h / TILE_SIZE);
return Vec2(((float)w / scale_) / TILE_SIZE, ((float)h / scale_) / TILE_SIZE);
}

SDL_Rect createDestRect(Vec2 pos, Vec2 pixsize) {
return SDL_Rect{
(int)((pos.x - cam_.x) * TILE_SIZE * scale_),
(int)((pos.y - cam_.y) * TILE_SIZE * scale_),
(int)(pixsize.x * scale_), (int)(pixsize.y * scale_),
};
}

struct ShowTextureArgs {
SDL_RendererFlip flip = SDL_FLIP_NONE;
double hscale = 1;
double vscale = 1;
double angle = 0;
std::optional<SDL_Point> center = std::nullopt;
};

void showTexture(
const Vec2 &pos, SDL_Texture *tex, SDL_Rect *srcrect,
SDL_RendererFlip flip = SDL_FLIP_NONE, double angle = 0) {
ShowTextureArgs args) {

SDL_Rect destrect{
(int)((pos.x - cam_.x) * TILE_SIZE), (int)((pos.y - cam_.y) * TILE_SIZE),
srcrect->w, srcrect->h,
};
SDL_Point *center = args.center ? &*args.center : nullptr;
SDL_Rect destrect = createDestRect(pos, Vec2(srcrect->w * args.hscale, srcrect->h * args.hscale));
if (SDL_RenderCopyEx(renderer_, tex, srcrect, &destrect, args.angle, center, args.flip) < 0)
warn << "RenderCopyEx failed: " << SDL_GetError();
}

if (SDL_RenderCopyEx(renderer_, tex, srcrect, &destrect, angle, NULL, flip) < 0) {
panic << "RenderCopy failed: " << SDL_GetError();
abort();
}
// We want an overload which uses RenderCopy instead of RenderCopyEx,
// because RenderCopy might be faster
void showTexture(const Vec2 &pos, SDL_Texture *tex, SDL_Rect *srcrect) {
SDL_Rect destrect = createDestRect(pos, Vec2(srcrect->w, srcrect->h));
if (SDL_RenderCopy(renderer_, tex, srcrect, &destrect) < 0)
warn << "RenderCopy failed: " << SDL_GetError();
}

void drawRect(const Vec2 &pos, const Vec2 &size) {
SDL_Rect destrect = createDestRect(pos, size * TILE_SIZE);
if (SDL_RenderDrawRect(renderer_, &destrect) < 0)
warn << "RenderDrawRect failed: " << SDL_GetError();
}

float scale_ = 2;

+ 1
- 1
libswan/src/Animation.cc View File

@@ -18,7 +18,7 @@ void Animation::tick(float dt) {

void Animation::draw(const Vec2 &pos, Win &win) {
SDL_Rect rect = resource_.frameRect(frame_);
win.showTexture(pos, resource_.texture_.get(), &rect, flip_);
win.showTexture(pos, resource_.texture_.get(), &rect, { .flip = flip_ });
}

void Animation::reset() {

+ 1
- 6
libswan/src/Body.cc View File

@@ -84,12 +84,7 @@ void Body::collideY(WorldPlane &plane) {
}

void Body::outline(Win &win) {
//win.setPos(pos_);
//sf::RectangleShape rect(size_ * TILE_SIZE);
//rect.setFillColor(sf::Color::Transparent);
//rect.setOutlineColor(sf::Color(128, 128, 128));
//rect.setOutlineThickness(1);
//win.draw(rect);
win.drawRect(pos_, size_);
}

void Body::update(WorldPlane &plane, float dt) {

+ 3
- 8
libswan/src/WorldPlane.cc View File

@@ -133,14 +133,9 @@ void WorldPlane::draw(Win &win) {
ent->draw(getContext(), win);

if (debug_boxes_.size() > 0) {
//sf::RectangleShape rect(Vec2(TILE_SIZE, TILE_SIZE));
//rect.setFillColor(sf::Color(60, 70, 200, 100));
//rect.setOutlineThickness(1);
//rect.setOutlineColor(sf::Color(50, 65, 170, 200));
//for (auto &pos: debug_boxes_) {
// win.setPos(pos);
// win.draw(rect);
//}
for (auto &pos: debug_boxes_) {
win.drawRect(pos, Vec2(1, 1));
}
}
}


Loading…
Cancel
Save