- [WIP] Fase 3 en process
This commit is contained in:
88
source/backends/SDL3/base.cpp
Normal file
88
source/backends/SDL3/base.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#include "backends/backend.h"
|
||||
#include "state.h"
|
||||
#include "mini/win/win.h"
|
||||
#include "mini/surf/surf.h"
|
||||
|
||||
namespace backend
|
||||
{
|
||||
state_t current_state = running;
|
||||
|
||||
void init() {
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD);
|
||||
//video::init();
|
||||
}
|
||||
|
||||
void quit() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void poll_events() {
|
||||
//[TODO]
|
||||
//if (update_mode==UPDATE_WAIT)
|
||||
// SDL_WaitEvent(NULL);
|
||||
//else if (update_mode==UPDATE_TIMEOUT)
|
||||
// SDL_WaitEventTimeout(NULL, timeout);
|
||||
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
current_state=quitting;
|
||||
break;
|
||||
}
|
||||
else if (e.type == SDL_EVENT_TEXT_INPUT) {
|
||||
SDL_strlcpy(input::key::text_input_buffer, e.text.text, sizeof(input::key::text_input_buffer));
|
||||
input::key::has_text_input = true;
|
||||
}
|
||||
else if (e.type == SDL_EVENT_KEY_DOWN) {
|
||||
#ifdef DEBUG
|
||||
if (e.key.scancode == SDL_SCANCODE_F12) {
|
||||
mini::surf::reloadsurfs();
|
||||
//} else if (e.key.scancode == SDL_SCANCODE_F11) {
|
||||
// mini::lua::debug::toggle();
|
||||
} else if (e.key.scancode == SDL_SCANCODE_F5) {
|
||||
current_state=exiting;
|
||||
} else {
|
||||
input::key::just_pressed = e.key.scancode;
|
||||
}
|
||||
#else
|
||||
input::key::just_pressed = e.key.scancode;
|
||||
#endif
|
||||
}
|
||||
else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
||||
if (input::mouse::discard_buttons)
|
||||
input::mouse::discard_buttons = false;
|
||||
else
|
||||
if (e.button.clicks==2 && e.button.button==SDL_BUTTON_LEFT) {
|
||||
input::mouse::double_click = true;
|
||||
} else {
|
||||
input::mouse::just_pressed = e.button.button;
|
||||
}
|
||||
}
|
||||
else if (e.type == SDL_EVENT_MOUSE_WHEEL) {
|
||||
input::mouse::w = e.wheel.y;
|
||||
}
|
||||
else if (e.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
||||
input::pad::just_pressed = e.gbutton.button;
|
||||
}
|
||||
}
|
||||
|
||||
input::key::keys = SDL_GetKeyboardState(NULL);
|
||||
|
||||
// Update mouse
|
||||
float real_mouse_x, real_mouse_y;
|
||||
input::mouse::buttons = SDL_GetMouseState(&real_mouse_x, &real_mouse_y);
|
||||
float mx, my;
|
||||
SDL_RenderCoordinatesFromWindow(video::renderer, real_mouse_x, real_mouse_y, &mx, &my);
|
||||
input::mouse::x = int(mx/mini::win::state.zoom);
|
||||
input::mouse::y = int(my/mini::win::state.zoom);
|
||||
}
|
||||
|
||||
const state_t& state() {
|
||||
return current_state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
153
source/backends/SDL3/input.cpp
Normal file
153
source/backends/SDL3/input.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#include "backends/backend.h"
|
||||
#include "state.h"
|
||||
#include "mini/view/view.h"
|
||||
|
||||
namespace backend
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
void reset() {
|
||||
key::just_pressed = 0;
|
||||
key::has_text_input = false;
|
||||
key::text_input_buffer[0] = '\0';
|
||||
mouse::just_pressed = 0;
|
||||
mouse::double_click = false;
|
||||
mouse::w = 0;
|
||||
pad::just_pressed = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
}
|
||||
|
||||
namespace key
|
||||
{
|
||||
const bool *keys;
|
||||
uint8_t just_pressed = 0;
|
||||
char text_input_buffer[10];
|
||||
bool has_text_input = false;
|
||||
|
||||
bool down(uint8_t i) {
|
||||
return keys[i];
|
||||
}
|
||||
|
||||
bool press(uint8_t i) {
|
||||
if (just_pressed == i) {
|
||||
just_pressed=0;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int press() {
|
||||
return just_pressed;
|
||||
}
|
||||
|
||||
bool any() {
|
||||
const bool something_pressed = (just_pressed != 0) || (pad::just_pressed != -1);
|
||||
just_pressed=0;
|
||||
pad::just_pressed=-1;
|
||||
return something_pressed;
|
||||
}
|
||||
|
||||
void text(const bool enable) {
|
||||
if (enable)
|
||||
SDL_StartTextInput(backend::video::window);
|
||||
else
|
||||
SDL_StopTextInput(backend::video::window);
|
||||
}
|
||||
|
||||
const char *utf8char() {
|
||||
return has_text_input ? text_input_buffer : nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
namespace mouse
|
||||
{
|
||||
int x, y, w;
|
||||
uint32_t buttons;
|
||||
uint8_t just_pressed = 0;
|
||||
bool double_click = false;
|
||||
bool discard_buttons = false;
|
||||
|
||||
int posx() {
|
||||
return x - mini::view::state.origin[0];
|
||||
}
|
||||
|
||||
int posy() {
|
||||
return y - mini::view::state.origin[1];
|
||||
}
|
||||
|
||||
int wheel() {
|
||||
return w;
|
||||
}
|
||||
|
||||
bool down(uint8_t i) {
|
||||
if (discard_buttons) return false;
|
||||
return buttons & SDL_BUTTON_MASK(i);
|
||||
}
|
||||
|
||||
bool press(uint8_t i) {
|
||||
return just_pressed == i;
|
||||
}
|
||||
|
||||
bool dblclick() {
|
||||
return double_click;
|
||||
}
|
||||
|
||||
void discard() {
|
||||
discard_buttons = true;
|
||||
}
|
||||
|
||||
bool inside(int x, int y, int w, int h) {
|
||||
const int mx = x - mini::view::state.origin[0];
|
||||
const int my = y - mini::view::state.origin[1];
|
||||
return (mx>=x) && (my>=y) && (mx<x+w) && (my<y+h);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace pad
|
||||
{
|
||||
SDL_Gamepad *gamepad = nullptr;
|
||||
int8_t just_pressed = -1;
|
||||
|
||||
void init() {
|
||||
int num_joysticks;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
|
||||
if (joysticks) {
|
||||
for (int i=0; i<num_joysticks; ++i) {
|
||||
if (SDL_IsGamepad(joysticks[i])) {
|
||||
gamepad = SDL_OpenGamepad(joysticks[i]);
|
||||
if (SDL_GamepadConnected(gamepad)) {
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
// [TODO]
|
||||
//log_msg(LOG_OK, "Gamepad found and initialized");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool down(int8_t i) {
|
||||
if (!gamepad) return false;
|
||||
return SDL_GetGamepadButton(gamepad, SDL_GamepadButton(i)) == 1;
|
||||
}
|
||||
|
||||
bool press(int8_t i) {
|
||||
if (just_pressed == i) {
|
||||
just_pressed = -1;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int press() {
|
||||
return just_pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
43
source/backends/SDL3/state.h
Normal file
43
source/backends/SDL3/state.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace backend
|
||||
{
|
||||
namespace video
|
||||
{
|
||||
extern SDL_Window *window;
|
||||
extern SDL_Renderer *renderer;
|
||||
extern SDL_Texture *tex_back;
|
||||
extern SDL_Texture *tex_shader;
|
||||
}
|
||||
|
||||
namespace input
|
||||
{
|
||||
namespace key
|
||||
{
|
||||
extern const bool *keys;
|
||||
extern uint8_t just_pressed;
|
||||
extern char text_input_buffer[10];
|
||||
extern bool has_text_input;
|
||||
}
|
||||
|
||||
namespace mouse
|
||||
{
|
||||
extern int x, y, w;
|
||||
extern uint32_t buttons;
|
||||
extern uint8_t just_pressed;
|
||||
extern bool double_click;
|
||||
extern bool discard_buttons;
|
||||
}
|
||||
|
||||
namespace pad
|
||||
{
|
||||
extern SDL_Gamepad *gamepad;
|
||||
extern int8_t just_pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
88
source/backends/SDL3/video.cpp
Normal file
88
source/backends/SDL3/video.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#include "backends/backend.h"
|
||||
#include "state.h"
|
||||
#include "mini/win/win.h"
|
||||
#include "mini/surf/surf.h"
|
||||
#include "mini/pal/pal.h"
|
||||
#include "mini/shader/shader.h"
|
||||
|
||||
namespace backend
|
||||
{
|
||||
namespace video
|
||||
{
|
||||
SDL_Window *window { nullptr };
|
||||
SDL_Renderer *renderer { nullptr };
|
||||
SDL_Texture *tex_back { nullptr };
|
||||
SDL_Texture *tex_shader { nullptr };
|
||||
|
||||
void init() {
|
||||
const auto &title = mini::win::state.title;
|
||||
const auto &width = mini::win::state.width;
|
||||
const auto &height = mini::win::state.height;
|
||||
auto &zoom = mini::win::state.zoom;
|
||||
|
||||
// Ajustar el zoom a un valor vàlid
|
||||
if (zoom <= 0) zoom = 1;
|
||||
const SDL_DisplayMode *dm = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
|
||||
while ( width * zoom > dm->w || height * zoom > dm->h) zoom--;
|
||||
|
||||
// Crear SDL_Window i SDL_Renderer
|
||||
window = SDL_CreateWindow(title, width*zoom, height*zoom, SDL_WINDOW_OPENGL|(mini::win::state.fullscreen?SDL_WINDOW_FULLSCREEN:0));
|
||||
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||
renderer = SDL_CreateRenderer(window, NULL);
|
||||
|
||||
// Mostrar o ocultar el cursor
|
||||
if (mini::win::state.cursor) SDL_ShowCursor(); else SDL_HideCursor();
|
||||
|
||||
// Crear textura backbuffer
|
||||
tex_back = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
|
||||
SDL_SetTextureScaleMode(tex_back, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
// Crear textura shaders i inicialitzar shaders
|
||||
tex_shader = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width*zoom, height*zoom);
|
||||
SDL_SetTextureScaleMode(tex_shader, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
mini::shader::init(window, tex_shader, nullptr);
|
||||
|
||||
// [TODO]
|
||||
//log_msg(LOG_OK, "Graphics subsystem initialized\n");
|
||||
}
|
||||
|
||||
void quit() {
|
||||
SDL_DestroyTexture(tex_shader);
|
||||
SDL_DestroyTexture(tex_back);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
void render() {
|
||||
// Render frame
|
||||
SDL_SetRenderTarget(renderer, tex_shader);
|
||||
//SDL_SetRenderDrawColor(win::state.renderer, 0, 0, 0, 255);
|
||||
//SDL_RenderClear(win::state.renderer);
|
||||
|
||||
uint32_t *pixels;
|
||||
int pitch;
|
||||
SDL_LockTexture(tex_back, NULL, (void**)&pixels, &pitch);
|
||||
for (uint32_t i=0;i<mini::surf::state.screen_surface->size;++i) pixels[i] = mini::pal::palette[mini::surf::state.screen_surface->p[i]];
|
||||
SDL_UnlockTexture(tex_back);
|
||||
SDL_RenderTexture(renderer, tex_back, NULL, NULL); //NEW
|
||||
|
||||
mini::shader::render();
|
||||
|
||||
//SDL_RenderTexture(mini_ren, mini_bak, NULL, NULL);
|
||||
//SDL_RenderPresent(mini_ren);
|
||||
}
|
||||
|
||||
void raise_window() {
|
||||
SDL_RaiseWindow(window);
|
||||
}
|
||||
|
||||
void cursor(const bool value) {
|
||||
if (value) SDL_ShowCursor(); else SDL_HideCursor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
16
source/backends/backend.cpp
Normal file
16
source/backends/backend.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "backend.h"
|
||||
#include <chrono>
|
||||
|
||||
namespace backend
|
||||
{
|
||||
uint64_t get_time_ms() {
|
||||
using namespace std::chrono;
|
||||
return duration_cast<milliseconds>(
|
||||
steady_clock::now().time_since_epoch()
|
||||
).count();
|
||||
}
|
||||
|
||||
void exit() {
|
||||
current_state = quitting;
|
||||
}
|
||||
}
|
||||
75
source/backends/backend.h
Normal file
75
source/backends/backend.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#define SDL3 1
|
||||
#define SFML 2
|
||||
#define EMSCRIPTEN 3
|
||||
|
||||
#ifndef BACKEND
|
||||
#define BACKEND SDL3
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace backend
|
||||
{
|
||||
enum state_t { running=0, exiting=1, quitting=2 };
|
||||
extern state_t current_state;
|
||||
|
||||
void init();
|
||||
void quit();
|
||||
void poll_events();
|
||||
const state_t& state();
|
||||
void exit();
|
||||
uint64_t get_time_ms();
|
||||
|
||||
namespace video
|
||||
{
|
||||
void init();
|
||||
void quit();
|
||||
void render();
|
||||
void raise_window();
|
||||
void cursor(const bool value);
|
||||
}
|
||||
|
||||
namespace audio
|
||||
{
|
||||
void init();
|
||||
void quit();
|
||||
void render();
|
||||
}
|
||||
|
||||
namespace input
|
||||
{
|
||||
void reset();
|
||||
|
||||
namespace key
|
||||
{
|
||||
bool down(uint8_t i);
|
||||
bool press(uint8_t i);
|
||||
int press();
|
||||
bool any();
|
||||
void text(const bool enable);
|
||||
const char *utf8char();
|
||||
}
|
||||
|
||||
namespace mouse
|
||||
{
|
||||
int posx();
|
||||
int posy();
|
||||
int wheel();
|
||||
bool down(uint8_t i);
|
||||
bool press(uint8_t i);
|
||||
bool dblclick();
|
||||
void discard();
|
||||
bool inside(int x, int y, int w, int h);
|
||||
}
|
||||
|
||||
namespace pad
|
||||
{
|
||||
void init();
|
||||
bool down(int8_t i);
|
||||
bool press(int8_t i);
|
||||
int press();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user