Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
SDL2: initial
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 24, 2023
1 parent 841ba07 commit 0a770af
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
2 changes: 2 additions & 0 deletions asan_supp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
leak:libX11.so.6
leak:XkbGetMap
71 changes: 15 additions & 56 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#include <SDL.h>
#include <SDL2/SDL_mixer.h>

#include "h3r_os_error.h"
H3R_ERR_DEFINE_UNHANDLED
H3R_ERR_DEFINE_HANDLER(Memory,H3R_ERR_HANDLER_UNHANDLED)
Expand Down Expand Up @@ -98,6 +95,8 @@ button click : Data_Heroes3_snd/BUTTON.wav

//LATER How hard is to replace this with a plug-in?
#ifdef SDL2_GL
#include <SDL.h>
#include <SDL2/SDL_mixer.h>
#include "h3r_sdlwindow.h"
#endif

Expand Down Expand Up @@ -192,17 +191,9 @@ int main(int argc, char ** argv)
#ifdef SDL2_GL
H3R_NS::SDLWindow * sdl_gl;
H3R_CREATE_OBJECT(sdl_gl, H3R_NS::SDLWindow) {};
#endif
H3R_NS::Window main_window
#ifdef SDL2_GL
{sdl_gl}
#endif
;
main_window.Show ();

/*if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_Init error: %s" EOL, SDL_GetError ())), 9;
"SDL_Init error: %s" EOL, SDL_GetError ())), 101;

int audio_result = 0;
int audio_flags = MIX_INIT_MP3;
Expand All @@ -212,50 +203,18 @@ int main(int argc, char ** argv)
if (-1 == Mix_OpenAudio (44100, AUDIO_S16SYS, 2, 1024))
return H3R_NS::Log::Info (H3R_NS::String::Format (
"Mix_OpenAudio error: %s" EOL, Mix_GetError ())), 9;
Mix_Music * music = Mix_LoadMUS (
"/mnt/workspace/rain/drive64_c/games/h3/MP3/MAINMENU.MP3");
Mix_Music * music = Mix_LoadMUS ("MP3/MAINMENU.MP3");
Mix_PlayMusic (music, -1);
bool q {false};
SDL_Event e;
SDL_Window * window = nullptr;
SDL_Surface * screenSurface = nullptr;
SDL_Surface * gHelloWorld = nullptr;
window = SDL_CreateWindow ("h3r",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (! window)
return H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_CreateWindow error: %s" EOL, SDL_GetError ())), 9;
screenSurface = SDL_GetWindowSurface (window);
gHelloWorld = SDL_LoadBMP (
"../h3r_unpacked/h3/Data_H3bitmap_lod/GamSelBk.bmp");
if (! gHelloWorld)
return H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_LoadBMP error: %s" EOL, SDL_GetError ())), 9;
while( !q ) {
while (SDL_PollEvent (&e) != 0) {
if (! q) q = SDL_QUIT == e.type;
if (SDL_WINDOWEVENT == e.type) {
H3R_NS::Log::Info ("SDL_WINDOWEVENT" EOL);
if (SDL_WINDOWEVENT_RESIZED == e.window.event)
if (e.window.data1 > 0 && e.window.data2 > 0)
screenSurface = SDL_GetWindowSurface (window);
}
}
//SDL_FillRect (screenSurface, nullptr,
// SDL_MapRGB (screenSurface->format, 0xaa, 0xaa, 0xaa));
//screenSurface = SDL_GetWindowSurface (window);
SDL_BlitSurface (gHelloWorld, nullptr, screenSurface, nullptr);
SDL_UpdateWindowSurface (window);
SDL_Delay (16);
}
#endif
H3R_NS::Window main_window
#ifdef SDL2_GL
{sdl_gl}
#endif
;
main_window.Show ();
#ifdef SDL2_GL
Mix_FreeMusic (music); // sounds nice :)
SDL_FreeSurface (gHelloWorld);
SDL_DestroyWindow (window);
SDL_Quit ();*/
SDL_Quit ();
#endif
return 0;
}
62 changes: 60 additions & 2 deletions os/ui/gl/sdl/h3r_sdlwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,75 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define _H3R_SDLWINDOW_H_

#include "h3r.h"
#include "h3r_log.h"
#include "h3r_oswindow.h"
#include <SDL.h>
#include <SDL2/SDL_mixer.h>

H3R_NAMESPACE

// SDL is a multi-OS bridge itself, so there is no need for OS-specific classes
// yet.
// Its this deep in directory tree on purpose.
// Its this deep in the directory tree on purpose.
//LATER plug-in; an SDL window shouldn't concern itself with h3r at all.
class SDLWindow : public OSWindow
{
protected: virtual void Open () override {}
SDL_Event e;
SDL_Window * _window {};
SDL_Surface * _screenSurface {};
SDL_Surface * _gHelloWorld {};
bool q {false};

protected: inline virtual void Open () override
{
_window = SDL_CreateWindow ("h3r",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (! _window) {
H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_CreateWindow error: %s" EOL, SDL_GetError ()));
return;
}
_screenSurface = SDL_GetWindowSurface (_window);
_gHelloWorld = SDL_LoadBMP (
"../h3r_unpacked/h3/Data_H3bitmap_lod/GamSelBk.bmp");
if (! _gHelloWorld) {
H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_LoadBMP error: %s" EOL, SDL_GetError ()));
return;
}

while (! q) {
while (SDL_PollEvent (&e) != 0) {
if (! q) q = SDL_QUIT == e.type;
if (SDL_WINDOWEVENT == e.type) {
H3R_NS::Log::Info ("SDL_WINDOWEVENT" EOL);
if (SDL_WINDOWEVENT_RESIZED == e.window.event)
if (e.window.data1 > 0 && e.window.data2 > 0)
_screenSurface = SDL_GetWindowSurface (_window);
}
if (SDL_KEYUP == e.type &&
SDL_SCANCODE_Q == e.key.keysym.scancode)
q = true;
}
// SDL_FillRect (screenSurface, nullptr,
// SDL_MapRGB (screenSurface->format, 0xaa, 0xaa, 0xaa));
// screenSurface = SDL_GetWindowSurface (window);
SDL_BlitSurface (_gHelloWorld, nullptr, _screenSurface, nullptr);
SDL_UpdateWindowSurface (_window);
SDL_Delay (16);
}
}// Open

public: SDLWindow()
{
}

public: ~SDLWindow() override
{
if (_gHelloWorld) SDL_FreeSurface (_gHelloWorld);
if (_window) SDL_DestroyWindow (_window);
}
};

NAMESPACE_H3R
Expand Down
1 change: 1 addition & 0 deletions os/ui/h3r_oswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ H3R_NAMESPACE
class OSWindow : public IWindow
{
public: virtual void Open () override {}
public: virtual ~OSWindow() {}
};

NAMESPACE_H3R
Expand Down
1 change: 1 addition & 0 deletions ui/h3r_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Window : public IWindow
protected: virtual void Open () override { _win->Open (); }
public: Window(OSWindow * actual_window) : _win{actual_window} {}
public: void Show() { Open (); }
public: ~Window() { H3R_DESTROY_OBJECT(_win, OSWindow) }
};

NAMESPACE_H3R
Expand Down

0 comments on commit 0a770af

Please sign in to comment.