Browse Source

slowly but surely approaching feature parity

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

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

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

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

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

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

@@ -4,17 +4,15 @@ EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF &params):
PhysicsEntity(SIZE, MASS),
anims_{
Swan::Animation(ctx.resources.getImage("core::player-still"), 0.8),
Swan::Animation(ctx.resources.getImage("core::player-running"), 1),
Swan::Animation(ctx.resources.getImage("core::player-running"), 1, Swan::Animation::Flags::HFLIP) } {
Swan::Animation(ctx.resources.getImage("core::player-running"), 1, SDL_FLIP_HORIZONTAL),
Swan::Animation(ctx.resources.getImage("core::player-running"), 1) } {

readSRF(ctx, params);
}

void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) {
body_.outline(win);

win.setPos(body_.pos_ - Swan::Vec2(0.2, 0.1));
anims_[(int)state_].draw(win);
//body_.outline(win);
anims_[(int)state_].draw(body_.pos_ - Swan::Vec2(0.2, 0.1), win);
}

void EntPlayer::update(const Swan::Context &ctx, float dt) {
@@ -53,7 +51,7 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) {
anims_[(int)state_].reset();
anims_[(int)state_].tick(dt);

Swan::PhysicsEntity::update(ctx, dt);
//Swan::PhysicsEntity::update(ctx, dt);
}

void EntPlayer::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) {

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

@@ -35,6 +35,6 @@ private:
State state_ = State::IDLE;
std::array<Swan::Animation, (int)State::COUNT> anims_;

Swan::Timer jump_timer_;
Swan::Clock jump_timer_;
Swan::TilePos mouse_tile_;
};

+ 2
- 2
core.mod/src/main.cc View File

@@ -11,8 +11,8 @@ extern "C" void mod_init(Swan::Mod &mod) {
mod.registerImage({ "stone", "tiles/stone.png" });
mod.registerImage({ "dirt", "tiles/dirt.png" });
mod.registerImage({ "grass", "tiles/grass.png" });
mod.registerImage({ "player-running", "entities/player-running.png" });
mod.registerImage({ "player-still", "entities/player-still.png" });
mod.registerImage({ "player-running", "entities/player-running.png", 64 });
mod.registerImage({ "player-still", "entities/player-still.png", 64 });

mod.registerTile({
.name = "air",

+ 6
- 9
libswan/include/swan/Animation.h View File

@@ -1,5 +1,7 @@
#pragma once

#include <SDL2/SDL.h>

#include "common.h"
#include "Resource.h"
#include "Clock.h"
@@ -9,24 +11,19 @@ namespace Swan {

class Animation {
public:
enum class Flags {
HFLIP = 1,
};

Animation(ImageResource &resource, float interval, Flags flags = (Flags)0):
resource_(resource), interval_(interval), timer_(interval), flags_(flags) {}
Animation(ImageResource &resource, float interval, SDL_RendererFlip flip = SDL_FLIP_NONE):
resource_(resource), interval_(interval), timer_(interval), flip_(flip) {}

void tick(float dt);
void draw(Win &win);
void draw(const Vec2 &pos, Win &win);
void reset();

private:
ImageResource &resource_;
float interval_;
float timer_;
Flags flags_;
SDL_RendererFlip flip_;
int frame_ = 0;
bool dirty_ = true;
};

}

+ 5
- 0
libswan/include/swan/Resource.h View File

@@ -26,6 +26,11 @@ public:

void tick(float dt);

SDL_Rect frameRect(int frame = -1) const {
if (frame == -1) frame = frame_;
return SDL_Rect{ 0, frame_height_ * frame, surface_->w, frame_height_ };
}

static std::unique_ptr<ImageResource> createInvalid(Win &win);

std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *)> surface_{nullptr, &SDL_FreeSurface};

+ 11
- 13
libswan/include/swan/Win.h View File

@@ -9,7 +9,7 @@ namespace Swan {

class Win {
public:
Win(SDL_Renderer *renderer): renderer_(renderer) {
Win(SDL_Window *window, SDL_Renderer *renderer): window_(window), renderer_(renderer) {
if (SDL_GetRendererInfo(renderer_, &rinfo_) < 0) {
panic << "GetRenedrerInfo failed: " << SDL_GetError();
abort();
@@ -18,25 +18,22 @@ public:
info << "Using renderer: " << rinfo_.name;
}

void setPos(const Vec2 &pos) {
//transform_ = sf::Transform()
// .scale(scale_, scale_)
// .translate((pos - cam_) * TILE_SIZE);
}

Vec2 getSize() {
//sf::Vector2u v = window_->getSize();
//return Vec2(v.x, v.y) / (TILE_SIZE * scale_);
return Vec2(10, 10);
int w, h;
SDL_GetWindowSize(window_, &w, &h);
return Vec2((float)w / TILE_SIZE, (float)h / TILE_SIZE);
}

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

SDL_Rect destrect{
(int)pos.x * TILE_SIZE, (int)pos.y * TILE_SIZE,
(int)((pos.x - cam_.x) * TILE_SIZE), (int)((pos.y - cam_.y) * TILE_SIZE),
srcrect->w, srcrect->h,
};

if (SDL_RenderCopy(renderer_, tex, srcrect, &destrect) < 0) {
if (SDL_RenderCopyEx(renderer_, tex, srcrect, &destrect, angle, NULL, flip) < 0) {
panic << "RenderCopy failed: " << SDL_GetError();
abort();
}
@@ -44,6 +41,7 @@ public:

float scale_ = 2;
Vec2 cam_;
SDL_Window *window_;
SDL_Renderer *renderer_;
SDL_RendererInfo rinfo_;
};

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

@@ -1,6 +1,7 @@
#include "Animation.h"

#include "Win.h"
#include "gfxutil.h"

namespace Swan {

@@ -12,18 +13,17 @@ void Animation::tick(float dt) {
frame_ += 1;
if (frame_ >= resource_.num_frames_)
frame_ = 0;

dirty_ = true;
}
}

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

void Animation::reset() {
timer_ = interval_;
frame_ = 0;
dirty_ = true;
}

}

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

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

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

+ 0
- 1
libswan/src/Chunk.cc View File

@@ -151,7 +151,6 @@ void Chunk::render(const Context &ctx) {
auto &srcsurf = tile->image_.surface_;
SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h };

//SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
destsurf->pixels = pixels + (y * TILE_SIZE * pitch) + x * TILE_SIZE * 4;
SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE };


+ 7
- 7
libswan/src/Resource.cc View File

@@ -30,7 +30,7 @@ ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) {
// If we have a surface, and it's the wrong pixel format, convert it
if (surface_ && surface_->format->format != format) {
info
<< builder.name << ": Converting from "
<< " " << builder.name << ": Converting from "
<< SDL_GetPixelFormatName(surface_->format->format) << " to "
<< SDL_GetPixelFormatName(format);
surface_.reset(SDL_ConvertSurfaceFormat(surface_.get(), format, 0));
@@ -43,7 +43,6 @@ ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) {

surface_.reset(SDL_CreateRGBSurface(
0, TILE_SIZE, TILE_SIZE, bpp, rmask, gmask, bmask, amask));

SDL_FillRect(surface_.get(), NULL, SDL_MapRGB(surface_->format,
PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE));
}
@@ -52,9 +51,7 @@ ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) {
if (frame_height_ < 0)
frame_height_ = surface_->h;

texture_.reset(SDL_CreateTexture(
renderer, surface_->format->format, SDL_TEXTUREACCESS_STATIC,
surface_->w, frame_height_));
texture_.reset(SDL_CreateTextureFromSurface(renderer, surface_.get()));
if (!texture_) {
panic << "CreateTexture failed: " << SDL_GetError();
abort();
@@ -72,8 +69,11 @@ ImageResource::ImageResource(
0, TILE_SIZE, TILE_SIZE, 32, 0, 0, 0, 0));
SDL_FillRect(surface_.get(), NULL, SDL_MapRGB(surface_->format, r, g, b));

texture_.reset(SDL_CreateTexture(
renderer, surface_->format->format, SDL_TEXTUREACCESS_STATIC, w, h));
texture_.reset(SDL_CreateTextureFromSurface(renderer, surface_.get()));
if (!texture_) {
panic << "CreateTexture failed: " << SDL_GetError();
abort();
}

frame_height_ = h;
num_frames_ = 1;

+ 1
- 1
src/main.cc View File

@@ -48,7 +48,7 @@ int main() {
SDL_DestroyRenderer);
sdlassert(renderer, "Could not create renderer");

Win win(renderer.get());
Win win(window.get(), renderer.get());

Game game(win);
std::vector<std::unique_ptr<Mod>> mods;

Loading…
Cancel
Save