Browse Source

more progress on sfml -> sdl

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

+ 1
- 6
CMakeLists.txt View File

@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.0)
project(swan)

find_package(SFML 2.5 COMPONENTS graphics system window REQUIRED)
find_package(ImGui-SFML REQUIRED)

set(CMAKE_CXX_CLANG_TIDY
clang-tidy
--header-filter=.*
@@ -27,9 +24,7 @@ endif()

add_subdirectory(third_party)

set(libraries
sfml-graphics sfml-system sfml-window sfml-audio ImGui-SFML::ImGui-SFML
imgui SDL2 SDL2_image dl z)
set(libraries imgui SDL2 SDL2_image dl z)

# We want to be able to use C++20 designated initializers,
# but Clang doesn't support them yet.

+ 0
- 2
libswan/include/swan/Body.h View File

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

#include <SFML/Graphics.hpp>

#include "common.h"
#include "BoundingBox.h"


+ 2
- 6
libswan/include/swan/Chunk.h View File

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

#include <SFML/Graphics/Texture.hpp>
#include <string.h>
#include <stdint.h>
#include <memory>
@@ -22,8 +21,6 @@ public:
Chunk(ChunkPos pos): pos_(pos) {
data_.reset(new uint8_t[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)]);
visuals_.reset(new Visuals());
visuals_->tex_.create(CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE);
visuals_->sprite_ = sf::Sprite(visuals_->tex_);
visuals_->dirty_ = false;
}

@@ -43,7 +40,7 @@ public:

private:
static constexpr float DEACTIVATE_INTERVAL = 20;
static sf::Uint8 *renderbuf;
static uint8_t *renderbuf;

bool isCompressed() { return compressed_size_ != -1; }

@@ -54,8 +51,7 @@ private:
float deactivate_timer_ = DEACTIVATE_INTERVAL;

struct Visuals {
sf::Texture tex_;
sf::Sprite sprite_;
std::unique_ptr<SDL_Texture, void (*)(SDL_Texture *)> texture_{nullptr, SDL_DestroyTexture};
bool dirty_;
};
std::unique_ptr<Visuals> visuals_;

+ 10
- 12
libswan/include/swan/Game.h View File

@@ -13,22 +13,20 @@ class Game {
public:
Game(Win &win):
win_(win),
mouse_pos_(0, 0),
keys_pressed_(sf::Keyboard::Key::KeyCount, false),
mouse_pressed_(sf::Mouse::Button::ButtonCount, false) {}
mouse_pos_(0, 0) {}

void createWorld(const std::string &worldgen);

void onKeyPressed(sf::Keyboard::Key key) { keys_pressed_[(int)key] = true; }
void onKeyReleased(sf::Keyboard::Key key) { keys_pressed_[(int)key] = false; }
void onMouseMove(int x, int y) { mouse_pos_ = Vec2i(x, y); }
void onMousePressed(sf::Mouse::Button button) { mouse_pressed_[(int)button] = true; }
void onMouseReleased(sf::Mouse::Button button) { mouse_pressed_[(int)button] = false; }
void onKeyDown(SDL_Keysym sym) { pressed_keys_[sym.scancode] = true; }
void onKeyUp(SDL_Keysym sym) { pressed_keys_[sym.scancode] = false; }
void onMouseMove(Sint32 x, Sint32 y) { mouse_pos_ = { x, y }; }
void onMouseDown(Sint32 x, Sint32 y, Uint8 button) { mouse_pos_ = { x, y }; pressed_buttons_[button] = true; }
void onMouseUp(Sint32 x, Sint32 y, Uint8 button) { mouse_pos_ = { x, y }; pressed_buttons_[button] = false; }

bool isKeyPressed(SDL_Scancode code) { return pressed_keys_[code]; }
TilePos getMouseTile();
Vec2i getMousePos() { return mouse_pos_; }
bool isKeyPressed(sf::Keyboard::Key key) { return keys_pressed_[(int)key]; }
bool isMousePressed(sf::Mouse::Button button) { return mouse_pressed_[(int)button]; }
bool isMousePressed(Uint8 button) { return pressed_buttons_[button]; }

void draw();
void update(float dt);
@@ -41,9 +39,9 @@ public:
Win &win_;

private:
std::unordered_map<SDL_Scancode, bool> pressed_keys_;
Vec2i mouse_pos_;
std::vector<bool> keys_pressed_;
std::vector<bool> mouse_pressed_;
std::unordered_map<Uint8, bool> pressed_buttons_;
};

}

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

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

#include <SFML/System/Vector2.hpp>
#include <utility>

namespace Swan {
@@ -18,10 +17,6 @@ struct Vector2 {
return *this;
}

constexpr operator sf::Vector2<T>() const {
return sf::Vector2<T>(x, y);
}

constexpr operator std::pair<T, T>() const {
return std::pair<T, T>(x, y);
}

+ 0
- 4
libswan/include/swan/Win.h View File

@@ -16,10 +16,6 @@ public:
// .translate((pos - cam_) * TILE_SIZE);
}

void draw(const sf::Drawable &drawable) {
//window_->draw(drawable, transform_);
}

Vec2 getSize() {
//sf::Vector2u v = window_->getSize();
//return Vec2(v.x, v.y) / (TILE_SIZE * scale_);

+ 0
- 3
libswan/include/swan/common.h View File

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

#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>

#include "Vector2.h"

namespace Swan {

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

@@ -85,11 +85,11 @@ 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);
//sf::RectangleShape rect(size_ * TILE_SIZE);
//rect.setFillColor(sf::Color::Transparent);
//rect.setOutlineColor(sf::Color(128, 128, 128));
//rect.setOutlineThickness(1);
//win.draw(rect);
}

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

+ 4
- 11
libswan/src/Chunk.cc View File

@@ -1,6 +1,5 @@
#include "Chunk.h"

#include <SFML/System/Clock.hpp>
#include <zlib.h>

#include "World.h"
@@ -35,8 +34,6 @@ void Chunk::compress() {
if (isCompressed())
return;

sf::Clock clock;

// We only need a fixed-length temp buffer;
// if the compressed data gets too big, there's no point in compressing
uint8_t dest[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
@@ -54,9 +51,8 @@ void Chunk::compress() {
visuals_.reset();
compressed_size_ = destlen;

fprintf(stderr, "Compressed chunk %i,%i from %lu bytes to %lu bytes in %.3fs.\n",
pos_.x, pos_.y, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), destlen,
clock.getElapsedTime().asSeconds());
fprintf(stderr, "Compressed chunk %i,%i from %lu bytes to %lu bytes.\n",
pos_.x, pos_.y, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), destlen);
} else if (ret == Z_BUF_ERROR) {
fprintf(stderr, "Didn't compress chunk %i,%i because compressing it would've made it bigger.\n",
pos_.x, pos_.y);
@@ -69,8 +65,6 @@ void Chunk::decompress() {
if (!isCompressed())
return;

sf::Clock clock;

uint8_t *dest = new uint8_t[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
uLongf destlen = CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID);
int ret = uncompress(
@@ -89,9 +83,8 @@ void Chunk::decompress() {
visuals_->sprite_ = sf::Sprite();
visuals_->dirty_ = true;
need_render_ = true;
fprintf(stderr, "Decompressed chunk %i,%i from %li bytes to %lu bytes in %.3fs.\n",
pos_.x, pos_.y, compressed_size_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID),
clock.getElapsedTime().asSeconds());
fprintf(stderr, "Decompressed chunk %i,%i from %li bytes to %lu bytes.\n",
pos_.x, pos_.y, compressed_size_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID));
compressed_size_ = -1;
}


+ 30
- 0
src/main.cc View File

@@ -63,6 +63,35 @@ int main() {
int fcount = 0;
int slowFrames = 0;
while (1) {
SDL_Event evt;
while (SDL_PollEvent(&evt)) {
switch (evt.type) {
case SDL_QUIT:
goto exit;
break;

case SDL_KEYDOWN:
game.onKeyDown(evt.key.keysym);
break;

case SDL_KEYUP:
game.onKeyUp(evt.key.keysym);
break;

case SDL_MOUSEMOTION:
game.onMouseMove(evt.motion.x, evt.motion.y);
break;

case SDL_MOUSEBUTTONDOWN:
game.onMouseDown(evt.button.x, evt.button.y, evt.button.button);
break;

case SDL_MOUSEBUTTONUP:
game.onMouseUp(evt.button.x, evt.button.y, evt.button.button);
break;
}
}

auto now = std::chrono::steady_clock::now();
std::chrono::duration<float> dur(prevTime - now);
prevTime = now;
@@ -101,5 +130,6 @@ int main() {
SDL_UpdateWindowSurface(window.get());
}

exit:
return EXIT_SUCCESS;
}

Loading…
Cancel
Save