198 lines
7.5 KiB
C++
198 lines
7.5 KiB
C++
#include <SDL3/SDL.h>
|
|
#include <SDL3_image/SDL_image.h>
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
|
|
|
#include "Animal.h"
|
|
#include "NewDialogs/DlgMainMenu.h"
|
|
#include "Sound.h"
|
|
|
|
using namespace std;
|
|
|
|
int main(int argc, char **argv) {
|
|
if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) {
|
|
SDL_Log("Could not init SDL %s", SDL_GetError());
|
|
}
|
|
|
|
if(!TTF_Init()) {
|
|
SDL_Log("TTF_Init: %s", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
SDL_Window *sdl_window = SDL_CreateWindow("Rousku", 10, 10, SDL_WINDOW_TRANSPARENT | SDL_WINDOW_BORDERLESS | SDL_WINDOW_UTILITY | SDL_WINDOW_ALWAYS_ON_TOP);
|
|
if (sdl_window == NULL) {
|
|
SDL_Log("error creating window");
|
|
return 1;
|
|
}
|
|
SDL_WindowID sdl_window_id = SDL_GetWindowID(sdl_window);
|
|
|
|
SDL_Window *sdl_menu_win = SDL_CreateWindow("Rouskun asetukset", 10, 10, SDL_WINDOW_HIDDEN);
|
|
if (sdl_menu_win == NULL) {
|
|
SDL_Log("error creating window");
|
|
return 1;
|
|
}
|
|
SDL_WindowID sdl_menu_win_id = SDL_GetWindowID(sdl_menu_win);
|
|
|
|
SDL_Renderer *sdl_renderer = SDL_CreateRenderer(sdl_window, NULL);
|
|
if (sdl_renderer == NULL) {
|
|
SDL_Log("error creating renderer");
|
|
}
|
|
|
|
SDL_Renderer *sdl_menu_renderer = SDL_CreateRenderer(sdl_menu_win, NULL);
|
|
if (sdl_menu_renderer == NULL) {
|
|
SDL_Log("error creating renderer");
|
|
}
|
|
|
|
Sound sound;
|
|
|
|
bool press = false;
|
|
Uint64 prev_press_ticks = 0;
|
|
float mouse_win_rel_x, mouse_win_rel_y;
|
|
SDL_Rect old_menu_size = { 10, 10, 0, 0 };
|
|
DlgMainMenu *dlg_menu = new DlgMainMenu();
|
|
|
|
Animal rousku(dlg_menu->getSkin());
|
|
|
|
int num_displays;
|
|
SDL_DisplayID *displays = SDL_GetDisplays(&num_displays);
|
|
SDL_Log("Found %d display(s)", num_displays);
|
|
for (unsigned i = 0; i < num_displays; i++) {
|
|
SDL_Log("Display %d", displays[i]);
|
|
const SDL_DisplayMode *dm = SDL_GetCurrentDisplayMode(displays[i]);
|
|
if (!dm) {
|
|
SDL_Log("Couldn't get display mode: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
SDL_Log("screen w %d h %d", dm->w, dm->h);
|
|
rousku.disp_pos.w = dm->w;
|
|
rousku.disp_pos.h = dm->h;
|
|
rousku.disp_pos.x = dm->w / 2;
|
|
rousku.disp_pos.y = dm->h / 2;
|
|
}
|
|
|
|
// Get MOUSE_DOWN event when clicking unfocused window
|
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
|
|
|
while (!dlg_menu->isAppExit()) {
|
|
SDL_Event event;
|
|
SDL_zero(event);
|
|
while (SDL_PollEvent(&event)) {
|
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
|
} else if (event.type == SDL_EVENT_KEY_UP) {
|
|
} else if (event.type == SDL_EVENT_QUIT) {
|
|
dlg_menu->setAppExit(true);
|
|
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
|
if (event.button.windowID == sdl_window_id) {
|
|
SDL_Log("btn down");
|
|
rousku.setSpecialState(hang);
|
|
press = true;
|
|
mouse_win_rel_x = event.button.x;
|
|
mouse_win_rel_y = event.button.y;
|
|
Uint64 ticks = SDL_GetTicks();
|
|
|
|
// show menu if double clicking
|
|
if (ticks - prev_press_ticks < 300) {
|
|
dlg_menu->setExit(false);
|
|
}
|
|
prev_press_ticks = ticks;
|
|
} else {
|
|
dlg_menu->onMousePress(event.button.x, event.button.y);
|
|
}
|
|
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
|
if (press) {
|
|
SDL_Log("btn up");
|
|
rousku.setSpecialState(drop);
|
|
rousku.resetTimes(SDL_GetTicks());
|
|
press = false;
|
|
} else {
|
|
dlg_menu->onMouseRelease(event.button.x, event.button.y);
|
|
}
|
|
} else if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
|
if (event.button.windowID == sdl_window_id) {
|
|
if (press) {
|
|
float motion_x, motion_y;
|
|
SDL_GetGlobalMouseState(&motion_x, &motion_y);
|
|
rousku.disp_pos.x = motion_x - mouse_win_rel_x;
|
|
rousku.disp_pos.y = motion_y - mouse_win_rel_y;
|
|
} else if (dlg_menu->isRunAwayFromMouse()) {
|
|
rousku.shouldRunAway(event.motion.x < (float) rousku.getImage()->w / 2 ? 1 : -1); // selecting direction to run in parameter
|
|
}
|
|
}
|
|
} else if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
|
|
if (event.window.windowID == sdl_menu_win_id) {
|
|
dlg_menu->setExit(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
string changed_setting = dlg_menu->getChangedSetting();
|
|
if (changed_setting == "skin_list") {
|
|
rousku.readSkin(dlg_menu->getSkin());
|
|
rousku.setSpecialState(drop);
|
|
}
|
|
Uint64 delay = rousku.step(SDL_GetTicks(), press);
|
|
if (rousku.hasStateChanged()) {
|
|
sound.closeSound(); // so it will be reloaded with right format
|
|
}
|
|
SDL_Surface *animal_sfe = rousku.getImage();
|
|
|
|
SDL_Texture *sdl_texture = SDL_CreateTexture(sdl_renderer, animal_sfe->format, SDL_TEXTUREACCESS_STREAMING, animal_sfe->w, animal_sfe->h);
|
|
if (!sdl_texture) {
|
|
SDL_Log("Couldn't create texture: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
SDL_SetWindowPosition(sdl_window, rousku.disp_pos.x, rousku.disp_pos.y);
|
|
SDL_SetWindowSize(sdl_window, animal_sfe->w, animal_sfe->h);
|
|
SDL_SetWindowShape(sdl_window, animal_sfe);
|
|
|
|
SDL_UpdateTexture(sdl_texture, NULL, animal_sfe->pixels, animal_sfe->pitch);
|
|
SDL_RenderClear(sdl_renderer);
|
|
SDL_RenderTexture(sdl_renderer, sdl_texture, NULL, NULL);
|
|
SDL_RenderPresent(sdl_renderer);
|
|
|
|
SDL_DestroyTexture(sdl_texture);
|
|
if (dlg_menu->isExit()) {
|
|
SDL_HideWindow(sdl_menu_win);
|
|
} else {
|
|
dlg_menu->setPreviewSurface(rousku.getPreviewSurface());
|
|
|
|
SDL_Surface *dlg_menu_sfe = dlg_menu->getSurface();
|
|
SDL_Texture *sdl_menu_texture = SDL_CreateTexture(sdl_menu_renderer, dlg_menu_sfe->format, SDL_TEXTUREACCESS_STREAMING, dlg_menu_sfe->w, dlg_menu_sfe->h);
|
|
if (!sdl_menu_texture) {
|
|
SDL_Log("Couldn't create texture: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
if (dlg_menu_sfe->w != old_menu_size.w || dlg_menu_sfe->h != old_menu_size.h) {
|
|
SDL_SetWindowSize(sdl_menu_win, dlg_menu_sfe->w, dlg_menu_sfe->h);
|
|
old_menu_size.w = dlg_menu_sfe->w;
|
|
old_menu_size.h = dlg_menu_sfe->h;
|
|
}
|
|
SDL_UpdateTexture(sdl_menu_texture, NULL, dlg_menu_sfe->pixels, dlg_menu_sfe->pitch);
|
|
SDL_RenderClear(sdl_menu_renderer);
|
|
SDL_RenderTexture(sdl_menu_renderer, sdl_menu_texture, NULL, NULL);
|
|
SDL_RenderPresent(sdl_menu_renderer);
|
|
|
|
SDL_DestroyTexture(sdl_menu_texture);
|
|
SDL_ShowWindow(sdl_menu_win);
|
|
}
|
|
|
|
if (dlg_menu->isEnableSound()) {
|
|
SoundData *sd = rousku.getSound();
|
|
sound.setSound(sd);
|
|
} else {
|
|
sound.closeSound();
|
|
}
|
|
|
|
SDL_SetWindowAlwaysOnTop(sdl_window, dlg_menu->isAlwaysOnTop()); // FIXME on false seems to stay behind tint2, but otherwis still stays on top of normal windows, maybe because of UTILITY-window?
|
|
|
|
SDL_Delay(delay);
|
|
}
|
|
|
|
SDL_DestroyWindow(sdl_window);
|
|
SDL_Quit();
|
|
TTF_Quit();
|
|
|
|
return 0;
|
|
}
|