'src/entities/PlayerEntity.cc', | 'src/entities/PlayerEntity.cc', | ||||
'src/entities/ItemStackEntity.cc', | 'src/entities/ItemStackEntity.cc', | ||||
name_prefix: '', | name_prefix: '', | ||||
name_suffix: 'so', | |||||
install: true, | install: true, | ||||
install_dir: 'core.mod', | install_dir: 'core.mod', | ||||
dependencies: [libswan, libperlinnoise]) | dependencies: [libswan, libperlinnoise]) |
#include "entities/PlayerEntity.h" | #include "entities/PlayerEntity.h" | ||||
#include "entities/ItemStackEntity.h" | #include "entities/ItemStackEntity.h" | ||||
#include <functional> | |||||
class CoreMod: public Swan::Mod { | class CoreMod: public Swan::Mod { | ||||
public: | public: | ||||
CoreMod(Swan::World &world): Swan::Mod("core") { | CoreMod(Swan::World &world): Swan::Mod("core") { | ||||
breakListener_ = world.evtTileBreak_.subscribe( | breakListener_ = world.evtTileBreak_.subscribe( | ||||
std::bind_front(&CoreMod::onTileBreak, this)); | |||||
[=](auto ...args) { return onTileBreak(args...); }); | |||||
registerSprite("entity/player-running"); | registerSprite("entity/player-running"); | ||||
registerSprite("entity/player-still"); | registerSprite("entity/player-still"); |
void clear(Color color = {}); | void clear(Color color = {}); | ||||
void flip(); | void flip(); | ||||
void onResize(int w, int h); | void onResize(int w, int h); | ||||
SwanCommon::Vec2i size() { return { w_, h_ }; } | |||||
SwanCommon::Vec2i size() { return size_; } | |||||
double pixelRatio() { return ratio_; } | |||||
SDL_Window *sdlWindow(); | SDL_Window *sdlWindow(); | ||||
private: | private: | ||||
std::unique_ptr<WindowState> state_; | std::unique_ptr<WindowState> state_; | ||||
int w_; | |||||
int h_; | |||||
SwanCommon::Vec2i size_; | |||||
double ratio_; | |||||
}; | }; | ||||
} | } |
#include <SDL_opengles2.h> | |||||
#include <GL/glew.h> | |||||
#include <SDL_opengl.h> |
'src/TileAtlas.cc', | 'src/TileAtlas.cc', | ||||
'src/util.cc', | 'src/util.cc', | ||||
'src/Window.cc', | 'src/Window.cc', | ||||
dependencies: [common, libtracy, libthreads, libsdl2, libgles2], | |||||
dependencies: [common, libtracy, libthreads, libsdl2, libgl, libglew], | |||||
install: true, | install: true, | ||||
include_directories: 'include/cygnet')) | include_directories: 'include/cygnet')) |
GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, | GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, | ||||
SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT, | SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT, | ||||
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles); | 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles); | ||||
glCheck(); | |||||
} else if constexpr (std::endian::native == std::endian::big) { | } else if constexpr (std::endian::native == std::endian::big) { | ||||
uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2]; | uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2]; | ||||
for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) { | for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) { | ||||
GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, | GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, | ||||
SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT, | SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT, | ||||
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf); | 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf); | ||||
glCheck(); | |||||
} | } | ||||
glCheck(); | |||||
return chunk; | return chunk; | ||||
} | } | ||||
#include "Window.h" | #include "Window.h" | ||||
#include <SDL.h> | #include <SDL.h> | ||||
#include <iostream> | |||||
#include "gl.h" | #include "gl.h" | ||||
#include "util.h" | #include "util.h" | ||||
}; | }; | ||||
Window::Window(const char *name, int w, int h): | Window::Window(const char *name, int w, int h): | ||||
state_(std::make_unique<WindowState>()), w_(w), h_(h) { | |||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |||||
state_(std::make_unique<WindowState>()) { | |||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); | |||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | ||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | ||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | ||||
glCheck(); | glCheck(); | ||||
makeCurrent(); | makeCurrent(); | ||||
glewInit(); | |||||
std::cerr << "OpenGL Version: " << glGetString(GL_VERSION) << '\n'; | |||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||||
glEnable(GL_BLEND); | glEnable(GL_BLEND); | ||||
glCheck(); | glCheck(); | ||||
} | } | ||||
void Window::onResize(int w, int h) { | void Window::onResize(int w, int h) { | ||||
w_ = w; | |||||
h_ = h; | |||||
int dw, dh; | int dw, dh; | ||||
SDL_GL_GetDrawableSize(state_->window, &dw, &dh); | SDL_GL_GetDrawableSize(state_->window, &dw, &dh); | ||||
glViewport(0, 0, dw, dh); | glViewport(0, 0, dw, dh); | ||||
glCheck(); | glCheck(); | ||||
size_ = {dw, dh}; | |||||
ratio_ = (double)dw / (double)w; | |||||
} | } | ||||
SDL_Window *Window::sdlWindow() { | SDL_Window *Window::sdlWindow() { |
namespace Cygnet::Shaders { | namespace Cygnet::Shaders { | ||||
const char *chunkVx = R"glsl( | const char *chunkVx = R"glsl( | ||||
precision mediump float; | |||||
uniform mat3 camera; | uniform mat3 camera; | ||||
uniform vec2 pos; | uniform vec2 pos; | ||||
attribute vec2 vertex; | attribute vec2 vertex; | ||||
)glsl"; | )glsl"; | ||||
const char *chunkFr = R"glsl( | const char *chunkFr = R"glsl( | ||||
precision mediump float; | |||||
#define TILE_SIZE 32.0 | #define TILE_SIZE 32.0 | ||||
#define CHUNK_WIDTH 64 | #define CHUNK_WIDTH 64 | ||||
#define CHUNK_HEIGHT 64 | #define CHUNK_HEIGHT 64 | ||||
)glsl"; | )glsl"; | ||||
const char *chunkShadowVx = R"glsl( | const char *chunkShadowVx = R"glsl( | ||||
precision mediump float; | |||||
#define CHUNK_WIDTH 64 | #define CHUNK_WIDTH 64 | ||||
#define CHUNK_HEIGHT 64 | #define CHUNK_HEIGHT 64 | ||||
)glsl"; | )glsl"; | ||||
const char *chunkShadowFr = R"glsl( | const char *chunkShadowFr = R"glsl( | ||||
precision mediump float; | |||||
varying vec2 v_texCoord; | varying vec2 v_texCoord; | ||||
uniform sampler2D tex; | uniform sampler2D tex; | ||||
)glsl"; | )glsl"; | ||||
const char *tileVx = R"glsl( | const char *tileVx = R"glsl( | ||||
precision mediump float; | |||||
uniform mat3 camera; | uniform mat3 camera; | ||||
uniform mat3 transform; | uniform mat3 transform; | ||||
attribute vec2 vertex; | attribute vec2 vertex; | ||||
)glsl"; | )glsl"; | ||||
const char *tileFr = R"glsl( | const char *tileFr = R"glsl( | ||||
precision mediump float; | |||||
#define TILE_SIZE 32.0 | #define TILE_SIZE 32.0 | ||||
varying vec2 v_tileCoord; | varying vec2 v_tileCoord; | ||||
)glsl"; | )glsl"; | ||||
const char *spriteVx = R"glsl( | const char *spriteVx = R"glsl( | ||||
precision mediump float; | |||||
#define TILE_SIZE 32.0 | #define TILE_SIZE 32.0 | ||||
uniform mat3 camera; | uniform mat3 camera; | ||||
)glsl"; | )glsl"; | ||||
const char *spriteFr = R"glsl( | const char *spriteFr = R"glsl( | ||||
precision mediump float; | |||||
varying vec2 v_texCoord; | varying vec2 v_texCoord; | ||||
uniform sampler2D tex; | uniform sampler2D tex; | ||||
)glsl"; | )glsl"; | ||||
const char *rectVx = R"glsl( | const char *rectVx = R"glsl( | ||||
precision mediump float; | |||||
uniform mat3 camera; | uniform mat3 camera; | ||||
uniform vec2 pos; | uniform vec2 pos; | ||||
uniform vec2 size; | uniform vec2 size; | ||||
)glsl"; | )glsl"; | ||||
const char *rectFr = R"glsl( | const char *rectFr = R"glsl( | ||||
precision mediump float; | |||||
#define THICKNESS 0.02 | #define THICKNESS 0.02 | ||||
varying vec2 v_coord; | varying vec2 v_coord; | ||||
)glsl"; | )glsl"; | ||||
const char *blendVx = R"glsl( | const char *blendVx = R"glsl( | ||||
precision mediump float; | |||||
attribute vec2 vertex; | attribute vec2 vertex; | ||||
attribute vec2 texCoord; | attribute vec2 texCoord; | ||||
varying vec2 v_texCoord; | varying vec2 v_texCoord; | ||||
)glsl"; | )glsl"; | ||||
const char *blendFr = R"glsl( | const char *blendFr = R"glsl( | ||||
precision mediump float; | |||||
varying vec2 v_texCoord; | varying vec2 v_texCoord; | ||||
uniform sampler2D tex; | uniform sampler2D tex; | ||||
libdl = meson.get_compiler('cpp').find_library('dl') | libdl = meson.get_compiler('cpp').find_library('dl') | ||||
libz = meson.get_compiler('cpp').find_library('z') | libz = meson.get_compiler('cpp').find_library('z') | ||||
libgles2 = meson.get_compiler('cpp').find_library('GLESv2') | |||||
libglew = dependency('glew') | |||||
libgl = dependency('OpenGL') | |||||
#libgl = meson.get_compiler('cpp').find_library('GLESv2') | |||||
libsdl2 = dependency('sdl2') | libsdl2 = dependency('sdl2') | ||||
libsdl2_image = dependency('SDL2_image') | libsdl2_image = dependency('SDL2_image') | ||||
libthreads = dependency('threads') | libthreads = dependency('threads') |
#include <chrono> | #include <chrono> | ||||
#include <ratio> | #include <ratio> | ||||
#include <SDL2/SDL.h> | |||||
#include <SDL2/SDL_image.h> | |||||
#include <SDL.h> | |||||
#include <SDL_image.h> | |||||
#include <string.h> | #include <string.h> | ||||
#include <imgui.h> | #include <imgui.h> | ||||
#include <imgui_sdl.h> | #include <imgui_sdl.h> | ||||
imguiIO.MousePos.x = (float)evt.motion.x; | imguiIO.MousePos.x = (float)evt.motion.x; | ||||
imguiIO.MousePos.y = (float)evt.motion.y; | imguiIO.MousePos.y = (float)evt.motion.y; | ||||
if (!imguiIO.WantCaptureMouse) */ | if (!imguiIO.WantCaptureMouse) */ | ||||
game.onMouseMove(evt.motion.x, evt.motion.y); | |||||
game.onMouseMove( | |||||
evt.motion.x * window.pixelRatio(), | |||||
evt.motion.y * window.pixelRatio()); | |||||
break; | break; | ||||
case SDL_MOUSEBUTTONDOWN: | case SDL_MOUSEBUTTONDOWN: | ||||
/* | /* | ||||
imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = true; | imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = true; | ||||
if (!imguiIO.WantCaptureMouse) */ | if (!imguiIO.WantCaptureMouse) */ | ||||
game.onMouseDown(evt.button.x, evt.button.y, evt.button.button); | |||||
game.onMouseDown( | |||||
evt.button.x * window.pixelRatio(), | |||||
evt.button.y * window.pixelRatio(), | |||||
evt.button.button); | |||||
break; | break; | ||||
case SDL_MOUSEBUTTONUP: | case SDL_MOUSEBUTTONUP: | ||||
/* | /* | ||||
imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = false; | imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = false; | ||||
if (!imguiIO.WantCaptureMouse) */ | if (!imguiIO.WantCaptureMouse) */ | ||||
game.onMouseUp(evt.button.x, evt.button.y, evt.button.button); | |||||
game.onMouseUp( | |||||
evt.button.x * window.pixelRatio(), | |||||
evt.button.y * window.pixelRatio(), | |||||
evt.button.button); | |||||
break; | break; | ||||
case SDL_MOUSEWHEEL: | case SDL_MOUSEWHEEL: |