- [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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,47 +2,34 @@
|
||||
#include "mini/pad/pad.h"
|
||||
#include "mini/win/win.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include "backends/backend.h"
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace key
|
||||
{
|
||||
state_t state;
|
||||
|
||||
bool down(uint8_t i) {
|
||||
return state.keys[i];
|
||||
return backend::input::key::down(i);
|
||||
}
|
||||
|
||||
bool press(uint8_t i) {
|
||||
if (state.just_pressed == i) {
|
||||
state.just_pressed=0;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return backend::input::key::press(i);
|
||||
}
|
||||
|
||||
int press() {
|
||||
return state.just_pressed;
|
||||
return backend::input::key::press();
|
||||
}
|
||||
|
||||
bool any() {
|
||||
const bool something_pressed = (state.just_pressed != 0) || (pad::state.just_pressed != -1);
|
||||
state.just_pressed=0;
|
||||
pad::state.just_pressed=-1;
|
||||
return something_pressed;
|
||||
return backend::input::key::any();
|
||||
}
|
||||
|
||||
void text(const bool enable) {
|
||||
if (enable)
|
||||
SDL_StartTextInput(win::state.window);
|
||||
else
|
||||
SDL_StopTextInput(win::state.window);
|
||||
backend::input::key::text(enable);
|
||||
}
|
||||
|
||||
const char* utf8char() {
|
||||
return state.has_text_input ? state.text_input_buffer : nullptr;
|
||||
return backend::input::key::utf8char();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,14 +5,6 @@ namespace mini
|
||||
{
|
||||
namespace key
|
||||
{
|
||||
struct state_t {
|
||||
const bool *keys;
|
||||
uint8_t just_pressed = 0;
|
||||
char text_input_buffer[10];
|
||||
bool has_text_input = false;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
bool down(uint8_t i);
|
||||
bool press(uint8_t i);
|
||||
int press();
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
#include "lua/lua.h"
|
||||
#include "aux/log.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
//#include <SDL3/SDL.h>
|
||||
#include "backends/backend.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -43,9 +44,6 @@ int fps=0;
|
||||
uint32_t last_update = 0;
|
||||
float delta_time = 0.0f;
|
||||
|
||||
bool should_exit = false;
|
||||
bool should_quit = false;
|
||||
|
||||
int16_t beats, num_beats = 0;
|
||||
|
||||
void createNewProject();
|
||||
@@ -96,7 +94,7 @@ namespace mini
|
||||
}
|
||||
|
||||
float time() {
|
||||
return float(SDL_GetTicks())/1000.0f;
|
||||
return float(backend::get_time_ms())/1000.0f;
|
||||
}
|
||||
|
||||
bool beat(int16_t i) {
|
||||
@@ -123,8 +121,7 @@ namespace mini
|
||||
}
|
||||
|
||||
void exit() {
|
||||
should_exit = true;
|
||||
should_quit = true;
|
||||
backend::exit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,10 +134,10 @@ using namespace mini;
|
||||
|
||||
int main(int argc,char*argv[]){
|
||||
|
||||
#ifdef DEBUG
|
||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
|
||||
log_msg(LOG_UNSALTED, "MINI v%s\n",MINI_VERSION);
|
||||
#endif
|
||||
//#ifdef DEBUG
|
||||
// SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
|
||||
// log_msg(LOG_UNSALTED, "MINI v%s\n",MINI_VERSION);
|
||||
//#endif
|
||||
|
||||
// Gestió de arguments
|
||||
// ===============================================================
|
||||
@@ -176,8 +173,7 @@ int main(int argc,char*argv[]){
|
||||
file::setresourcefilename(res_file);
|
||||
#endif
|
||||
|
||||
while (!should_quit) {
|
||||
should_exit=false;
|
||||
while (backend::state()!=backend::quitting) {
|
||||
|
||||
// READ INI
|
||||
if (!override_ini)
|
||||
@@ -196,7 +192,7 @@ int main(int argc,char*argv[]){
|
||||
if (sound_enabled) audio::sound::enable::set(strcmp(sound_enabled, "true")==0?true:false);
|
||||
}
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD);
|
||||
backend::init();
|
||||
|
||||
log_msg(LOG_INFO, "STARTING A SYSTEM REINITIALIZATION\n");
|
||||
win::init();
|
||||
@@ -211,85 +207,24 @@ int main(int argc,char*argv[]){
|
||||
lua::init(main_lua_file);
|
||||
lua::callbacks::init();
|
||||
|
||||
Uint32 dt=SDL_GetTicks();
|
||||
uint32_t dt = backend::get_time_ms();
|
||||
|
||||
key::state.just_pressed = 0;
|
||||
key::state.has_text_input = false;
|
||||
pad::state.just_pressed = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
mouse::state.just_pressed = 0;
|
||||
mouse::state.wheel = 0;
|
||||
mouse::state.double_click = false;
|
||||
backend::input::reset();
|
||||
|
||||
int fps_counter=0;
|
||||
uint32_t fps_timer=0;
|
||||
|
||||
while(!should_exit) {
|
||||
if (update_mode==UPDATE_WAIT)
|
||||
SDL_WaitEvent(NULL);
|
||||
else if (update_mode==UPDATE_TIMEOUT)
|
||||
SDL_WaitEventTimeout(NULL, timeout);
|
||||
while(backend::state()==backend::running) {
|
||||
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
should_exit=true;
|
||||
should_quit=true;
|
||||
break;
|
||||
}
|
||||
else if (e.type == SDL_EVENT_TEXT_INPUT) {
|
||||
SDL_strlcpy(key::state.text_input_buffer, e.text.text, sizeof(key::state.text_input_buffer));
|
||||
key::state.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) {
|
||||
should_exit=true;
|
||||
} else {
|
||||
key::state.just_pressed = e.key.scancode;
|
||||
}
|
||||
#else
|
||||
key::state.just_pressed = e.key.scancode;
|
||||
#endif
|
||||
}
|
||||
else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
||||
if (mouse::state.discard)
|
||||
mouse::state.discard = false;
|
||||
else
|
||||
if (e.button.clicks==2 && e.button.button==SDL_BUTTON_LEFT) {
|
||||
mouse::state.double_click = true;
|
||||
} else {
|
||||
mouse::state.just_pressed = e.button.button;
|
||||
}
|
||||
}
|
||||
else if (e.type == SDL_EVENT_MOUSE_WHEEL) {
|
||||
mouse::state.wheel = e.wheel.y;
|
||||
}
|
||||
else if (e.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
||||
pad::state.just_pressed = e.gbutton.button;
|
||||
}
|
||||
}
|
||||
|
||||
key::state.keys = SDL_GetKeyboardState(NULL);
|
||||
|
||||
// Update mouse
|
||||
float real_mouse_x, real_mouse_y;
|
||||
mouse::state.buttons = SDL_GetMouseState(&real_mouse_x, &real_mouse_y);
|
||||
float mx, my;
|
||||
SDL_RenderCoordinatesFromWindow(win::state.renderer, real_mouse_x, real_mouse_y, &mx, &my);
|
||||
mouse::state.x = int(mx/win::state.zoom);
|
||||
mouse::state.y = int(my/win::state.zoom);
|
||||
backend::poll_events();
|
||||
|
||||
// Do update
|
||||
if (SDL_GetTicks()-dt>13) {
|
||||
dt = SDL_GetTicks();
|
||||
if (backend::get_time_ms()-dt>13) {
|
||||
dt = backend::get_time_ms();
|
||||
|
||||
if (lua::running()) {
|
||||
delta_time = float(SDL_GetTicks() - last_update)/1000.0f;
|
||||
last_update = SDL_GetTicks();
|
||||
delta_time = float(backend::get_time_ms() - last_update)/1000.0f;
|
||||
last_update = backend::get_time_ms();
|
||||
|
||||
lua::callbacks::update();
|
||||
} else {
|
||||
@@ -299,50 +234,25 @@ int main(int argc,char*argv[]){
|
||||
if (beats == 0) beats = num_beats;
|
||||
if (beats > 0) beats--;
|
||||
|
||||
// Reset keyboard, mouse and pad
|
||||
key::state.just_pressed = 0;
|
||||
key::state.has_text_input = false;
|
||||
key::state.text_input_buffer[0] = '\0';
|
||||
mouse::state.just_pressed = 0;
|
||||
mouse::state.double_click = false;
|
||||
mouse::state.wheel = 0;
|
||||
pad::state.just_pressed = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
backend::input::reset();
|
||||
}
|
||||
|
||||
// Render frame
|
||||
SDL_SetRenderTarget(win::state.renderer, win::state.tex_shader);
|
||||
//SDL_SetRenderDrawColor(win::state.renderer, 0, 0, 0, 255);
|
||||
//SDL_RenderClear(win::state.renderer);
|
||||
|
||||
uint32_t *pixels;
|
||||
int pitch;
|
||||
SDL_LockTexture(win::state.tex_back, NULL, (void**)&pixels, &pitch);
|
||||
for (uint32_t i=0;i<surf::state.screen_surface->size;++i) pixels[i] = pal::palette[surf::state.screen_surface->p[i]];
|
||||
SDL_UnlockTexture(win::state.tex_back);
|
||||
SDL_RenderTexture(win::state.renderer, win::state.tex_back, NULL, NULL); //NEW
|
||||
|
||||
shader::render();
|
||||
//SDL_RenderTexture(mini_ren, mini_bak, NULL, NULL);
|
||||
//SDL_RenderPresent(mini_ren);
|
||||
backend::video::render();
|
||||
|
||||
fps_counter++;
|
||||
if (SDL_GetTicks()>=(fps_timer+1000)) {
|
||||
if (backend::get_time_ms()>=(fps_timer+1000)) {
|
||||
fps = fps_counter;
|
||||
fps_counter=0;
|
||||
fps_timer = SDL_GetTicks();
|
||||
fps_timer = backend::get_time_ms();
|
||||
}
|
||||
}
|
||||
|
||||
lua::quit();
|
||||
audio::quit();
|
||||
|
||||
//Mix_Quit();
|
||||
|
||||
surf::quit();
|
||||
|
||||
map::quit();
|
||||
win::quit();
|
||||
SDL_Quit();
|
||||
backend::quit();
|
||||
}
|
||||
|
||||
lua::cleanup();
|
||||
|
||||
@@ -1,47 +1,42 @@
|
||||
#include "mouse.h"
|
||||
#include "mini/view/view.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include "backends/backend.h"
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace mouse
|
||||
{
|
||||
state_t state;
|
||||
|
||||
int posx() {
|
||||
return state.x-view::state.origin[0];
|
||||
return backend::input::mouse::posx();
|
||||
}
|
||||
|
||||
int posy() {
|
||||
return state.y-view::state.origin[1];
|
||||
return backend::input::mouse::posy();
|
||||
}
|
||||
|
||||
int wheel() {
|
||||
return state.wheel;
|
||||
return backend::input::mouse::wheel();
|
||||
}
|
||||
|
||||
bool down(uint8_t i) {
|
||||
if (state.discard) return false;
|
||||
return state.buttons & SDL_BUTTON_MASK(i);
|
||||
return backend::input::mouse::down(i);
|
||||
}
|
||||
|
||||
bool press(uint8_t i) {
|
||||
return state.just_pressed == i;
|
||||
return backend::input::mouse::press(i);
|
||||
}
|
||||
|
||||
bool dblclick() {
|
||||
return state.double_click;
|
||||
return backend::input::mouse::dblclick();
|
||||
}
|
||||
|
||||
void discard() {
|
||||
state.discard = true;
|
||||
backend::input::mouse::discard();
|
||||
}
|
||||
|
||||
bool inside(int x, int y, int w, int h) {
|
||||
const int mx = state.x - view::state.origin[0];
|
||||
const int my = state.y - view::state.origin[1];
|
||||
return (mx>=x) && (my>=y) && (mx<x+w) && (my<y+h);
|
||||
return backend::input::mouse::inside(x,y,w,h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,6 @@ namespace mini
|
||||
{
|
||||
namespace mouse
|
||||
{
|
||||
struct state_t {
|
||||
int x, y, wheel;
|
||||
uint32_t buttons;
|
||||
uint8_t just_pressed = 0;
|
||||
bool double_click = false;
|
||||
bool discard = false;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
int posx();
|
||||
int posy();
|
||||
int wheel();
|
||||
|
||||
@@ -1,48 +1,25 @@
|
||||
#include "pad.h"
|
||||
|
||||
#include "aux/log.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include "backends/backend.h"
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace pad
|
||||
{
|
||||
state_t state;
|
||||
|
||||
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])) {
|
||||
state.gamepad = SDL_OpenGamepad(joysticks[i]);
|
||||
if (SDL_GamepadConnected(state.gamepad)) {
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
log_msg(LOG_OK, "Gamepad found and initialized");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
backend::input::pad::init();
|
||||
}
|
||||
|
||||
bool down(int8_t i) {
|
||||
if (!state.gamepad) return false;
|
||||
return SDL_GetGamepadButton(state.gamepad, SDL_GamepadButton(i)) == 1;
|
||||
return backend::input::pad::down(i);
|
||||
}
|
||||
|
||||
bool press(int8_t i) {
|
||||
if (state.just_pressed == i) {
|
||||
state.just_pressed = -1;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return backend::input::pad::press(i);
|
||||
}
|
||||
|
||||
int press() {
|
||||
return state.just_pressed;
|
||||
return backend::input::pad::press();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct SDL_Gamepad;
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace pad
|
||||
{
|
||||
struct state_t {
|
||||
SDL_Gamepad *gamepad = nullptr;
|
||||
int8_t just_pressed = -1;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
void init();
|
||||
bool down(int8_t i);
|
||||
bool press(int8_t i);
|
||||
|
||||
@@ -92,7 +92,8 @@ namespace mini
|
||||
|
||||
char *fshaderfile = nullptr;
|
||||
if (fshader) { fshaderfile = file::getfilebuffer(fshader, filesize, true); }
|
||||
init(win::state.window, win::state.tex_shader, vshaderfile, fshaderfile);
|
||||
// [TODO]
|
||||
//init(win::state.window, win::state.tex_shader, vshaderfile, fshaderfile);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
#include "aux/log.h"
|
||||
#include "mini/file/file.h"
|
||||
#include "mini/shader/shader.h"
|
||||
#include <backends/backend.h>
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace mini
|
||||
{
|
||||
@@ -12,48 +13,16 @@ namespace mini
|
||||
state_t state;
|
||||
|
||||
void init() {
|
||||
// Ajustar el zoom a un valor vàlid
|
||||
if (state.zoom <= 0) state.zoom = 1;
|
||||
const SDL_DisplayMode *dm = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
|
||||
while (state.width*state.zoom > dm->w || state.height*state.zoom > dm->h) state.zoom--;
|
||||
|
||||
// Crear SDL_Window i SDL_Renderer
|
||||
state.window = SDL_CreateWindow(state.title, state.width*state.zoom, state.height*state.zoom, SDL_WINDOW_OPENGL|(state.fullscreen?SDL_WINDOW_FULLSCREEN:0));
|
||||
//windowID = SDL_GetWindowID(mini_win);
|
||||
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||
state.renderer = SDL_CreateRenderer(state.window, NULL);
|
||||
|
||||
// Mostrar o ocultar el cursor
|
||||
if (state.cursor) SDL_ShowCursor(); else SDL_HideCursor();
|
||||
|
||||
// Crear textura backbuffer
|
||||
state.tex_back = SDL_CreateTexture(state.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, state.width, state.height);
|
||||
SDL_SetTextureScaleMode(state.tex_back, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
//SDL_PropertiesID props = SDL_GetTextureProperties(state.tex_back);
|
||||
//int real_pixelformat = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_FORMAT_NUMBER, -1);
|
||||
//if (real_pixelformat != SDL_PIXELFORMAT_ARGB8888) {
|
||||
// log_msg(LOG_FAIL, "Pixelformat incorrecte: %i\n", real_pixelformat);
|
||||
// exit(1);
|
||||
//}
|
||||
|
||||
// Crear textura shaders i inicialitzar shaders
|
||||
state.tex_shader = SDL_CreateTexture(state.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, state.width*state.zoom, state.height*state.zoom);
|
||||
SDL_SetTextureScaleMode(state.tex_shader, SDL_SCALEMODE_NEAREST);
|
||||
shader::init(state.window, state.tex_shader, nullptr);
|
||||
|
||||
backend::video::init();
|
||||
log_msg(LOG_OK, "Graphics subsystem initialized\n");
|
||||
}
|
||||
|
||||
void quit() {
|
||||
SDL_DestroyTexture(state.tex_shader);
|
||||
SDL_DestroyTexture(state.tex_back);
|
||||
SDL_DestroyRenderer(state.renderer);
|
||||
SDL_DestroyWindow(state.window);
|
||||
backend::video::quit();
|
||||
}
|
||||
|
||||
void raise() {
|
||||
SDL_RaiseWindow(state.window);
|
||||
backend::video::raise_window();
|
||||
}
|
||||
|
||||
namespace zoom {
|
||||
@@ -64,8 +33,7 @@ namespace mini
|
||||
state.zoom = value;
|
||||
quit();
|
||||
init();
|
||||
char strzoom[3];
|
||||
file::setconfigvalue("zoom", SDL_itoa(state.zoom, strzoom, 10));
|
||||
file::setconfigvalue("zoom", std::to_string(state.zoom).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +55,7 @@ namespace mini
|
||||
}
|
||||
void set(const bool value) {
|
||||
state.cursor=value;
|
||||
if (state.cursor) SDL_ShowCursor(); else SDL_HideCursor();
|
||||
backend::video::cursor(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct SDL_Window;
|
||||
struct SDL_Renderer;
|
||||
struct SDL_Texture;
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace win
|
||||
@@ -16,12 +12,6 @@ namespace mini
|
||||
uint8_t zoom { 4 };
|
||||
bool fullscreen { false };
|
||||
bool cursor { true };
|
||||
|
||||
SDL_Window *window { nullptr };
|
||||
SDL_Renderer*renderer { nullptr };
|
||||
SDL_Texture *tex_back { nullptr };
|
||||
SDL_Texture *tex_shader { nullptr };
|
||||
//Uint32 windowID;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user