mirror of
https://github.com/ondra-novak/gates_of_skeldal.git
synced 2025-07-20 13:15:16 -04:00
import sdl, some tests, nothing work yet
This commit is contained in:
parent
a7278bac40
commit
378b5586ab
37 changed files with 721 additions and 167 deletions
6
platform/CMakeLists.txt
Normal file
6
platform/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
SET(files error.cpp legacy_coroutines.cpp)
|
||||
|
||||
add_library(skeldal_platform_libs ${files})
|
||||
set_property(TARGET skeldal_platform_libs PROPERTY CXX_STANDARD 20)
|
||||
|
||||
add_subdirectory(sdl)
|
|
@ -1,34 +0,0 @@
|
|||
#ifndef _FCS_TASKER_H_
|
||||
#define _FCS_TASKER_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef void (*TaskerFunctionName)(va_list);
|
||||
|
||||
void tasker(EVENT_MSG *msg,void **data);
|
||||
int create_task();
|
||||
int add_task(int stack,TaskerFunctionName fcname,...);
|
||||
void term_task(int id_num);
|
||||
char is_running(int id_num);
|
||||
void suspend_task(int id_num,int msg);
|
||||
void shut_down_task(int id_num);
|
||||
void unsuspend_task(EVENT_MSG *msg);
|
||||
void *task_sleep(void *data);
|
||||
void *task_wait_event(int32_t event_number);
|
||||
int q_any_task();
|
||||
char task_quitmsg();
|
||||
char task_quitmsg_by_id(int id);
|
||||
char q_is_mastertask();
|
||||
int q_current_task();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1 @@
|
|||
|
9
platform/error.cpp
Normal file
9
platform/error.cpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <iostream>
|
||||
extern "C" {
|
||||
#include "error.h"
|
||||
}
|
||||
|
||||
|
||||
void display_error(const char *text) {
|
||||
std::cerr << "ERROR:" << text << std::endl;
|
||||
}
|
3
platform/error.h
Normal file
3
platform/error.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void display_error(const char *text);
|
141
platform/legacy_coroutines.cpp
Normal file
141
platform/legacy_coroutines.cpp
Normal file
|
@ -0,0 +1,141 @@
|
|||
#include "legacy_coroutines.h"
|
||||
|
||||
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
struct TaskInfo {
|
||||
int id;
|
||||
std::thread thr;
|
||||
std::atomic<bool> resume_flag = {false};
|
||||
std::chrono::system_clock::time_point _wake_up_after = {};
|
||||
int wake_up_msg = -1;
|
||||
bool request_exit = false;
|
||||
|
||||
TaskInfo(int id):id(id) {}
|
||||
};
|
||||
|
||||
using TaskList = std::unordered_map<int, std::unique_ptr<TaskInfo> >;
|
||||
static int next_task_id = 1;
|
||||
static TaskList task_list;
|
||||
|
||||
static int get_new_task_id() {
|
||||
int id = next_task_id;
|
||||
next_task_id = (next_task_id + 1) & 0xFFFF;
|
||||
if (task_list.find(id) != task_list.end()) id = get_new_task_id();
|
||||
return id;
|
||||
}
|
||||
|
||||
static std::atomic<bool> resume_master_flag = {false};
|
||||
static TaskInfo *current_task_inst = NULL;
|
||||
static EVENT_MSG *cur_message = NULL;
|
||||
|
||||
static void switch_to_task(TaskInfo *task) {
|
||||
if (task == current_task_inst) return;
|
||||
if (task == NULL) {
|
||||
TaskInfo *me = current_task_inst;
|
||||
current_task_inst = NULL;
|
||||
me->resume_flag = false;
|
||||
resume_master_flag = true;
|
||||
resume_master_flag.notify_all();
|
||||
me->resume_flag.wait(false);
|
||||
} else if (current_task_inst == NULL) {
|
||||
current_task_inst = task;
|
||||
resume_master_flag = false;
|
||||
task->resume_flag = true;
|
||||
task->resume_flag.notify_all();
|
||||
resume_master_flag.wait(false);
|
||||
} else {
|
||||
TaskInfo *me = current_task_inst;
|
||||
me->resume_flag = false;
|
||||
task->resume_flag = true;
|
||||
task->resume_flag.notify_all();
|
||||
me->resume_flag.wait(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void clean_up_current_task() {
|
||||
TaskInfo *me = current_task_inst;
|
||||
if (!me) return;
|
||||
int id = me->id;
|
||||
me->thr.detach();
|
||||
task_list.erase(id);
|
||||
current_task_inst = NULL;
|
||||
resume_master_flag = true;
|
||||
resume_master_flag.notify_all();
|
||||
}
|
||||
|
||||
int add_task(int stack,TaskerFunctionName fcname,...) {
|
||||
int id = get_new_task_id();
|
||||
auto st = task_list.emplace(id, std::make_unique<TaskInfo>(id));
|
||||
TaskInfo *new_task = st.first->second.get();
|
||||
va_list args;
|
||||
va_start(args, fcname);
|
||||
new_task->thr = std::thread([&]{
|
||||
new_task->resume_flag.wait(false);
|
||||
fcname(args);
|
||||
clean_up_current_task();
|
||||
});
|
||||
switch_to_task(new_task);
|
||||
return id;
|
||||
}
|
||||
|
||||
void term_task(int id_num) {
|
||||
auto iter = task_list.find(id_num);
|
||||
if (iter != task_list.end()) {
|
||||
iter->second->request_exit = true;
|
||||
switch_to_task(iter->second.get());
|
||||
}
|
||||
}
|
||||
char is_running(int id_num) {
|
||||
return id_num < 0 || task_list.find(id_num) != task_list.end();
|
||||
}
|
||||
void unsuspend_task(EVENT_MSG *msg) {
|
||||
for (auto &[id, task]: task_list) {
|
||||
if (task->wake_up_msg == msg->msg) {
|
||||
EVENT_MSG cpy;
|
||||
cpy.msg = msg->msg;
|
||||
va_copy(cpy.data, msg->data);
|
||||
cur_message = &cpy;
|
||||
switch_to_task(task.get());
|
||||
va_end(cpy.data);
|
||||
cur_message = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
void task_sleep(void) {
|
||||
if (current_task_inst) {
|
||||
switch_to_task(NULL);
|
||||
} else {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
for (auto &[id, task]: task_list) {
|
||||
if (task->_wake_up_after < now && task->wake_up_msg == -1) {
|
||||
switch_to_task(task.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
EVENT_MSG *task_wait_event(int32_t event_number) {
|
||||
if (current_task_inst == NULL) return NULL;
|
||||
current_task_inst->wake_up_msg = event_number;
|
||||
switch_to_task(NULL);
|
||||
return NULL;
|
||||
}
|
||||
int q_any_task() {
|
||||
return task_list.size();
|
||||
}
|
||||
char task_quitmsg() {
|
||||
if (current_task_inst == NULL) return 0;
|
||||
return current_task_inst->request_exit?1:0;
|
||||
}
|
||||
char q_is_mastertask() {
|
||||
return current_task_inst == NULL;
|
||||
}
|
||||
int q_current_task() {
|
||||
return current_task_inst?current_task_inst->id:-1;
|
||||
}
|
||||
void task_sleep_for(unsigned int time_ms) {
|
||||
|
||||
}
|
68
platform/legacy_coroutines.h
Normal file
68
platform/legacy_coroutines.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
#ifndef _FCS_TASKER_H_
|
||||
#define _FCS_TASKER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event.h>
|
||||
|
||||
|
||||
typedef void (*TaskerFunctionName)(va_list);
|
||||
|
||||
//void tasker(EVENT_MSG *msg,void **data);
|
||||
//int create_task();
|
||||
///start new task
|
||||
/**
|
||||
* @param stack stack size - ignored by some platforms
|
||||
* @param fcname function to start
|
||||
* @param ... arguments
|
||||
* @return id of task
|
||||
*
|
||||
* @note task is started immediatelly.
|
||||
* @note arguments are valid only until task is suspended
|
||||
*/
|
||||
int add_task(int stack,TaskerFunctionName fcname,...);
|
||||
///terminate task
|
||||
/**
|
||||
* request to terminate given task. The task can check this flag by
|
||||
* task_quitmsg()
|
||||
|
||||
*/
|
||||
void term_task(int id_num);
|
||||
///returns true, if task is running
|
||||
char is_running(int id_num);
|
||||
//void suspend_task(int id_num,int msg);
|
||||
//void shut_down_task(int id_num);
|
||||
///resumes task waiting on given message event
|
||||
|
||||
void unsuspend_task(EVENT_MSG *msg);
|
||||
|
||||
///sleep current task and switch to another
|
||||
void task_sleep(void);
|
||||
|
||||
void task_sleep_for(unsigned int time_ms);
|
||||
///sleep current task and wake up when given event is triggered
|
||||
/**
|
||||
* @param event_number waiting number
|
||||
* @return received message, NULL if task_term called
|
||||
*/
|
||||
EVENT_MSG *task_wait_event(int32_t event_number);
|
||||
///returns count of tasks
|
||||
int q_any_task();
|
||||
///returns 1 if terminate is requested
|
||||
char task_quitmsg();
|
||||
//char task_quitmsg_by_id(int id);
|
||||
char q_is_mastertask();
|
||||
///returns id of current task
|
||||
int q_current_task();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1
platform/sdl/CMakeLists.txt
Normal file
1
platform/sdl/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
|||
add_subdirectory(tests)
|
16
platform/sdl/tests/CMakeLists.txt
Normal file
16
platform/sdl/tests/CMakeLists.txt
Normal file
|
@ -0,0 +1,16 @@
|
|||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests/)
|
||||
|
||||
set(testFiles sdl_minimal_test.cpp sdl_surface_test.cpp
|
||||
)
|
||||
|
||||
foreach (testFile ${testFiles})
|
||||
string(REGEX MATCH "([^\/]+$)" filename ${testFile})
|
||||
string(REGEX MATCH "[^.]*" executable_name test_${filename})
|
||||
add_executable(${executable_name} ${testFile})
|
||||
target_link_libraries(${executable_name} ${SDL2_LIBRARIES} )
|
||||
add_test(NAME ${executable_name} COMMAND ${executable_name})
|
||||
endforeach ()
|
||||
|
||||
|
||||
add_executable(sdl_fullscreen_window sdl_fullscreen_window.cpp)
|
||||
target_link_libraries(sdl_fullscreen_window ${SDL2_LIBRARIES} )
|
57
platform/sdl/tests/sdl_fullscreen_window.cpp
Normal file
57
platform/sdl/tests/sdl_fullscreen_window.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <iostream>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Window* window = SDL_CreateWindow("Fullscreen Toggle with Aspect Ratio",
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
640, 480, SDL_WINDOW_RESIZABLE);
|
||||
|
||||
if (!window) {
|
||||
SDL_Log("Unable to create window: %s", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
SDL_Log("Unable to create renderer: %s", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
int isFullscreen = 0;
|
||||
int running = 1;
|
||||
SDL_Event event;
|
||||
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = 0;
|
||||
}
|
||||
if (event.type == SDL_KEYDOWN) {
|
||||
if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT)) {
|
||||
// Přepnutí mezi fullscreen a oknem
|
||||
isFullscreen = !isFullscreen;
|
||||
SDL_SetWindowFullscreen(window, isFullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Renderování
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
// Uklid zdrojů
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
27
platform/sdl/tests/sdl_minimal_test.cpp
Normal file
27
platform/sdl/tests/sdl_minimal_test.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
std::cerr << "SDL Initialization Error: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Window* window = SDL_CreateWindow("SDL Window",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
800, 600,
|
||||
SDL_WINDOW_SHOWN);
|
||||
|
||||
if (!window) {
|
||||
std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Delay(500); // Zobrazí okno na 3 sekundy
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
73
platform/sdl/tests/sdl_surface_test.cpp
Normal file
73
platform/sdl/tests/sdl_surface_test.cpp
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 480
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Inicializace SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
fprintf(stderr, "Chyba při inicializaci SDL: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření SDL okna
|
||||
SDL_Window *window = SDL_CreateWindow("DOS Game Port", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Chyba při vytváření okna: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření rendereru (pouze pro zobrazení)
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
fprintf(stderr, "Chyba při vytváření rendereru: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření SDL Surface (backbuffer)
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||
if (!texture) {
|
||||
fprintf(stderr, "Chyba při vytváření surface: %s\n", SDL_GetError());
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *raw_pixels;
|
||||
int pitch;
|
||||
SDL_LockTexture(texture, NULL, &raw_pixels, &pitch);
|
||||
// Přímý přístup do paměti surface
|
||||
Uint16 *pixels = reinterpret_cast<Uint16 *>(raw_pixels);
|
||||
printf("Adresa pixelů: %p, pitch: %d bajtů\n", (void *)pixels, pitch);
|
||||
|
||||
// Software rendering - příklad kreslení do paměti
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
Uint16 color = (x ^ y) & 0xFFFF; // Příklad barvy
|
||||
pixels[y * (pitch / 2) + x] = color; // Zápis pixelu
|
||||
}
|
||||
}
|
||||
|
||||
SDL_UnlockTexture(texture);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_Delay(3000); // Zobrazení okna na 3 sekundy
|
||||
|
||||
// Uvolnění zdrojů
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue