Browse Source

well, it compiles now

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
f7b79d94ac

+ 0
- 1
libcygnet/CMakeLists.txt View File

add_library(libcygnet SHARED add_library(libcygnet SHARED
src/Context.cc
src/GlWrappers.cc src/GlWrappers.cc
src/Renderer.cc src/Renderer.cc
src/ResourceManager.cc src/ResourceManager.cc

+ 0
- 11
libcygnet/include/cygnet/Context.h View File

#pragma once

namespace Cygnet {

class Context {
public:
Context();
~Context();
};

}

+ 4
- 0
libcygnet/include/cygnet/Window.h View File

#include <swan-common/Vector2.h> #include <swan-common/Vector2.h>
#include <memory> #include <memory>


struct SDL_Window;

namespace Cygnet { namespace Cygnet {


struct WindowState; struct WindowState;
void onResize(int w, int h); void onResize(int w, int h);
SwanCommon::Vec2i size() { return { w_, h_ }; } SwanCommon::Vec2i size() { return { w_, h_ }; }


SDL_Window *getWindow();

private: private:
std::unique_ptr<WindowState> state_; std::unique_ptr<WindowState> state_;
int w_; int w_;

+ 0
- 27
libcygnet/src/Context.cc View File

#include "Context.h"

#include <SDL.h>

#include "util.h"

namespace Cygnet {

Context::Context() {
SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);

SDL_GL_SetSwapInterval(1);
}

Context::~Context() {
SDL_Quit();
}

}

+ 13
- 0
libcygnet/src/Window.cc View File



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) { state_(std::make_unique<WindowState>()), w_(w), h_(h) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);

SDL_GL_SetSwapInterval(1);
state_->window = SDL_CreateWindow(name, state_->window = SDL_CreateWindow(name,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE |
glCheck(); glCheck();
} }


SDL_Window *Window::getWindow() {
return state_->window;
}

} }

+ 2
- 2
src/cygnet-test.cc View File

#include <cygnet/Context.h>
#include <cygnet/Window.h> #include <cygnet/Window.h>
#include <cygnet/Renderer.h> #include <cygnet/Renderer.h>
#include <cygnet/ResourceManager.h> #include <cygnet/ResourceManager.h>
} }


int main() { int main() {
Cygnet::Context ctx;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG); IMG_Init(IMG_INIT_PNG);
Cygnet::Window win("Cygnet Test", 680, 680); Cygnet::Window win("Cygnet Test", 680, 680);
Cygnet::Renderer rnd; Cygnet::Renderer rnd;


exit: exit:
IMG_Quit(); IMG_Quit();
SDL_Quit();
} }

+ 13
- 26
src/main.cc View File

#include <string.h> #include <string.h>
#include <imgui.h> #include <imgui.h>
#include <imgui_sdl/imgui_sdl.h> #include <imgui_sdl/imgui_sdl.h>
#include <cygnet/Renderer.h>
#include <cygnet/Window.h>


#include <swan/swan.h> #include <swan/swan.h>


imgassert(IMG_Init(imgFlags) == imgFlags, "Could not initialize SDL_Image"); imgassert(IMG_Init(imgFlags) == imgFlags, "Could not initialize SDL_Image");
Deferred<IMG_Quit> sdlImage; Deferred<IMG_Quit> sdlImage;


// Create the window
CPtr<SDL_Window, SDL_DestroyWindow> window(
SDL_CreateWindow(
"Project: SWAN",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
(int)(640 * guiScale), (int)(480 * guiScale), winFlags));
Cygnet::Window window("Project: SWAN", 640 * guiScale, 480 * guiScale);


// Load and display application icon // Load and display application icon
CPtr<SDL_Surface, SDL_FreeSurface> icon( CPtr<SDL_Surface, SDL_FreeSurface> icon(
IMG_Load("assets/icon.png")); IMG_Load("assets/icon.png"));
sdlassert(icon, "Could not load icon"); sdlassert(icon, "Could not load icon");
SDL_SetWindowIcon(window.get(), icon.get());

CPtr<SDL_Renderer, SDL_DestroyRenderer> renderer(
SDL_CreateRenderer(window.get(), -1, renderFlags));
sdlassert(renderer, "Could not create renderer");
SDL_SetRenderDrawBlendMode(renderer.get(), SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 255);

Win win(window.get(), renderer.get(), guiScale);
SDL_SetWindowIcon(window.getWindow(), icon.get());


// Init ImGUI and ImGUI_SDL // Init ImGUI and ImGUI_SDL
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
CPtr<ImGuiContext, ImGui::DestroyContext> context( CPtr<ImGuiContext, ImGui::DestroyContext> context(
ImGui::CreateContext()); ImGui::CreateContext());

/*
ImGuiSDL::Initialize(renderer.get(), (int)win.getPixSize().x, (int)win.getPixSize().y); ImGuiSDL::Initialize(renderer.get(), (int)win.getPixSize().x, (int)win.getPixSize().y);
Deferred<ImGuiSDL::Deinitialize> imguiSDL; Deferred<ImGuiSDL::Deinitialize> imguiSDL;
info << "Initialized with window size " << win.getPixSize(); info << "Initialized with window size " << win.getPixSize();
TODO */


// ImGuiIO is to glue SDL and ImGUI together // ImGuiIO is to glue SDL and ImGUI together
ImGuiIO& imguiIO = ImGui::GetIO(); ImGuiIO& imguiIO = ImGui::GetIO();
imguiIO.BackendPlatformName = "imgui_sdl + Project: SWAN"; imguiIO.BackendPlatformName = "imgui_sdl + Project: SWAN";


// Create a world // Create a world
Game game(win);
Game game;
std::vector<std::string> mods{ "core.mod" }; std::vector<std::string> mods{ "core.mod" };
game.createWorld("core::default", mods); game.createWorld("core::default", mods);


int slowFrames = 0; int slowFrames = 0;
while (1) { while (1) {
ZoneScopedN("game loop"); ZoneScopedN("game loop");
RTClock totalTimeClock;


SDL_Event evt; SDL_Event evt;
while (SDL_PollEvent(&evt)) { while (SDL_PollEvent(&evt)) {
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) { if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
imguiIO.DisplaySize.x = (float)evt.window.data1; imguiIO.DisplaySize.x = (float)evt.window.data1;
imguiIO.DisplaySize.y = (float)evt.window.data2; imguiIO.DisplaySize.y = (float)evt.window.data2;
win.onResize(evt.window.data1, evt.window.data2);
window.onResize(evt.window.data1, evt.window.data2);
} }
break; break;


} }


// Simple case: we can keep up, only need one physics update // Simple case: we can keep up, only need one physics update
RTClock updateClock;
if (dt <= 1 / 25.0) { if (dt <= 1 / 25.0) {
ZoneScopedN("game update"); ZoneScopedN("game update");
game.update(dt); game.update(dt);
while (tickAcc >= 1.0 / TICK_RATE) { while (tickAcc >= 1.0 / TICK_RATE) {
ZoneScopedN("game tick"); ZoneScopedN("game tick");
tickAcc -= 1.0 / TICK_RATE; tickAcc -= 1.0 / TICK_RATE;
RTClock tickClock;
game.tick(1.0 / TICK_RATE); game.tick(1.0 / TICK_RATE);
} }


{ {
auto [r, g, b, a] = game.backgroundColor();
RenderDrawColor c(renderer.get(), r, g, b, a);
SDL_RenderClear(renderer.get());
//auto [r, g, b, a] = game.backgroundColor();
// TODO: Set clear color
window.clear();
} }


// ImGUI // ImGUI


{ {
ZoneScopedN("game draw"); ZoneScopedN("game draw");
RTClock drawClock;
game.draw(); game.draw();
} }


ImGuiSDL::Render(ImGui::GetDrawData()); ImGuiSDL::Render(ImGui::GetDrawData());
} }


RTClock presentClock;
{ {
ZoneScopedN("render present"); ZoneScopedN("render present");
SDL_RenderPresent(renderer.get());
window.flip();
} }
FrameMark FrameMark
} }

Loading…
Cancel
Save