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

Commit

Permalink
window: the puzzle pieces are clicking in place
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 26, 2023
1 parent 7991390 commit a9f0f4f
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 158 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MAKEFLAGS += rR

APP = main
PLATFORM ?= posix
WIN_SYSTEM ?= gl/sdl -DSDL2_GL
WIN_SYSTEM ?= gl/sdl
CC ?= clang
CXX ?= clang++
H3R_TEST ?=
Expand All @@ -52,6 +52,7 @@ _I = -I. -Ios -Ios/$(PLATFORM) -Iutils -Iui -Istream -Iasync -Igame \
CXXFLAGS = $(_I) -std=c++11 $(_O) $(_F) $(_W)
SRC = $(wildcard ./*/*.cpp)
SRC += $(wildcard os/$(PLATFORM)/*.cpp)
SRC += $(wildcard os/ui/$(WIN_SYSTEM)/*.cpp)
SRC := $(filter-out ./prior_publish/%,$(SRC))
OBJ = $(patsubst %.cpp,%.o,$(SRC))

Expand Down
4 changes: 2 additions & 2 deletions h3r.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//LATER inttypes.h
static_assert(sizeof(int) >= 4, "This program expects 32-bit int at least");

// #include <new>
// #include < new >
#define H3R_CREATE_OBJECT(P,T) H3R_NS::OS::Alloc (P); new (P) T
#define H3R_DESTROY_OBJECT(P,T) \
if (nullptr != P) { P->~T (); H3R_NS::OS::Free (P); }
{ if (nullptr != P) { P->~T (); H3R_NS::OS::Free (P); } }

H3R_NAMESPACE

Expand Down
20 changes: 19 additions & 1 deletion h3r_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ H3R_LOG_STATIC_INIT
#include "h3r_sndfs.h"
#include "h3r_vidfs.h"

// No plug-in interface yet, so
#include "h3r_sdlwindow.h"

#include "h3r_mainwindow.h"

H3R_NAMESPACE

TaskThread Game::IOThread {};
Expand Down Expand Up @@ -80,7 +85,6 @@ Game::Game(const char * process_path)
Game::~Game()
{
H3R_DESTROY_OBJECT(Game::RM, ResManager)
H3R_DESTROY_OBJECT(MainWindow, IWindow)
}

void Game::SilentLog(bool v)
Expand All @@ -104,4 +108,18 @@ void Game::SilentLog(bool v)
return task_info.Resource;
}

#include <new>

int Game::Run(int argc, char ** argv)
{
// create the main window
// Again, no plug-in interface yet, so
var main_window =
IWindow::Create<H3R_NS::MainWindow, H3R_NS::SDLWindow>(argc, argv);

Game::MainWindow = main_window;
main_window->Show (); // make it visible
return ui_main (argc, argv);
}

NAMESPACE_H3R
4 changes: 3 additions & 1 deletion h3r_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Game final
// or R&D on parallelism, or whatever; because: short and simple.
public: static TaskThread IOThread;

public: static ResManager * RM;
public: static ResManager * RM;//PERHAPS this doesn't need to be public

public: static Stream * GetResource(const String & name);

Expand Down Expand Up @@ -158,6 +158,8 @@ class Game final
public: int Files() const { return _files; }
public: int Directories() const { return _dirs; }
}; // ResManagerInit

public: int Run(int, char **);
};// Game

NAMESPACE_H3R
Expand Down
86 changes: 2 additions & 84 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,57 +44,10 @@ H3R_ERR_DEFINE_HANDLER(File,H3R_ERR_HANDLER_UNHANDLED)
H3R_ERR_DEFINE_HANDLER(Log,H3R_ERR_HANDLER_UNHANDLED)

#include "h3r_game.h"
#include "h3r_filestream.h"

/*main menu
background : Data_H3bitmap_lod/GamSelBk.pcx
right overlay : Data_H3bitmap_lod/MainMenu.pcx
Looks the same as the rightmost portion of GamSelBk. Redraw
reasons? Layout?
"new game" : Data_H3sprite_lod/MMENUNG.def 1 blk 4 sprites; 2 bonus
Type: 71
Block[0]
name[0]: "mmenungn.pcx" up
name[1]: "mmenungs.pcx" down
name[2]: "mmenungh.pcx" highlighted
name[3]: "mmenungh.pcx"
The 2 bonus ones do look like replicating 0:0 and 0:1.
0:2 and 0:3 do look the same, and do have equivalent names.
SOMAIN.DEF has type 71 as well
Block [0]
name[0]: "SOMainS.pcx" down
name[1]: "SOMainH.pcx" up
The game has no reference to "mmenungh" - how does it know what to use on mouse
over? No reference to "SOMainS" and "mmenungn" - SOMAIN.DEF and MMENUNG.def have
"up" and "down" reversed - index-wise - how does it "know" whats what?
it counts on the last char prior the . it seems:
n - up
s - down
d - deactivated
h - hover
"new game" : Data_H3sprite_lod/MMENUNG.def
"load game" : Data_H3sprite_lod/MMENULG.def ditto
"high score" : Data_H3sprite_lod/MMENUHS.def ditto
"credits" : Data_H3sprite_lod/MMENUCR.def ditto
"quit" : Data_H3sprite_lod/MMENUQT.def ditto
main menu music : MP3/MAINMENU.MP3
button click : Data_Heroes3_snd/BUTTON.wav
*/

#include "h3r_window.h"
#include "h3r_oswindow.h"

//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

#include <new>

int main(int argc, char ** argv)
{
//TODO Path::GetDirName
H3R_ENSURE(argc > 0, "Can't handle your OS - argc can't be < 1")
H3R_NS::OS::Log_stdout ("Process: %s" EOL, argv[0]);
char * p = argv[0]; // the future base dir or work dir
Expand All @@ -107,40 +60,5 @@ int main(int argc, char ** argv)
H3R_NS::OS::Log_stdout ("WorkDir: %s" EOL, p);

H3R_NS::Game game {p};

#ifdef SDL2_GL
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 ())), 101;

//TODO audio init; requres OSFS
int audio_result = 0;
int audio_flags = MIX_INIT_MP3;
if (audio_flags != (audio_result = Mix_Init (audio_flags)))
return H3R_NS::Log::Info (H3R_NS::String::Format (
"Mix_Init error: %s" EOL, Mix_GetError ())), 9;
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 ("MP3/MAINMENU.MP3");
Mix_PlayMusic (music, -1);

H3R_NS::SDLWindow * sdl_gl;
H3R_CREATE_OBJECT(sdl_gl, H3R_NS::SDLWindow) {};
#endif
H3R_NS::Window * main_window;
H3R_CREATE_OBJECT(main_window, H3R_NS::Window)
#ifdef SDL2_GL
{sdl_gl}
#endif
;

H3R_NS::Game::MainWindow = main_window;
main_window->Show ();

#ifdef SDL2_GL
Mix_FreeMusic (music); // sounds nice :)
SDL_Quit ();
#endif
return 0;
return game.Run (argc, argv);
}
20 changes: 14 additions & 6 deletions os/h3r_os_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#define H3R_ERR_HANDLER_UNHANDLED H3R_NS::OS::Error::Unhandled
#define H3R_ERR_DEFINE_UNHANDLED H3R_NS::OS::Error H3R_ERR_HANDLER_UNHANDLED;
#define H3R_ERR_DEFINE_HANDLER(H,R) \
#define H3R_ERR_DEFINE_HANDLER(H, R) \
H3R_NS::OS::Error & H3R_NS::OS::Error::H = R;

#include "h3r.h"

H3R_NAMESPACE

#ifdef H3R_TEST
#define H3R_THROW(E,M) throw E {__FILE__, __LINE__, M};
#define H3R_THROW(E, M) throw E {__FILE__, __LINE__, M};
class Exception
{
private: const char * _file;
Expand All @@ -67,15 +67,23 @@ class NotSupportedException : public Exception
{
public: using Exception::Exception;
};
// Remind that some method isn't implemented.
// Way better than the pure virtual ...
class NotImplementedException : public Exception
{
public: using Exception::Exception;
};

// H3R_TEST
#else
#define H3R_THROW(E,M) H3R_ENSURE(false, "Unhandled Exception " #E ": " M)
#define H3R_THROW(E, M) H3R_ENSURE(false, "Unhandled Exception " #E ": " M)
#endif

#define H3R_THROW_IF(C,E,M) { if ((C)) H3R_THROW(E,M) }
#define H3R_ARG_EXC_IF(C,M) H3R_THROW_IF(C,ArgumentException,M)
#define H3R_NOT_SUPPORTED_EXC(M) H3R_THROW(NotSupportedException,M)
#define H3R_THROW_IF(C, E, M) { if ((C)) H3R_THROW(E, M) }
#define H3R_ARG_EXC_IF(C, M) H3R_THROW_IF(C,ArgumentException, M)
#define H3R_NOT_SUPPORTED_EXC(M) H3R_THROW(NotSupportedException, M)
#define H3R_NOT_IMPLEMENTED_EXC \
H3R_THROW(NotImplementedException, "Implement me.")

namespace OS {

Expand Down
92 changes: 92 additions & 0 deletions os/ui/gl/sdl/h3r_sdlwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,99 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#include "h3r_sdlwindow.h"
#include <SDL2/SDL_mixer.h>

struct SDL_Release final
{
Mix_Music * _music;
SDL_Release(Mix_Music * m) : _music{m} {}
~SDL_Release() { if (_music) Mix_FreeMusic (_music); SDL_Quit (); }
};

// Something like this shall be done at the plug-in code.
static bool global_sdl_init {};
static bool global_sdl_mix_init {};
static void Init_SDL()
{
if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
H3R_NS::Log::Err (H3R_NS::String::Format (
"SDL_Init error: %s. Stop." EOL, SDL_GetError ()));
H3R_NS::OS::Exit (1);
}
global_sdl_init = true;

//TODO audio init; requires OS VFS
int audio_result = 0;
int audio_flags = MIX_INIT_MP3;
if (audio_flags != (audio_result = Mix_Init (audio_flags))) {
H3R_NS::Log::Err (H3R_NS::String::Format (
"Mix_Init error: %s. The rest is silence." EOL, Mix_GetError ()));
return;
}
if (-1 == Mix_OpenAudio (44100, AUDIO_S16SYS, 2, 1024)) {
H3R_NS::Log::Info (H3R_NS::String::Format (
"Mix_OpenAudio error: %s. The rest is silence." EOL,
Mix_GetError ()));
return;
}
Mix_Music * music = Mix_LoadMUS ("MP3/MAINMENU.MP3");
if (music) Mix_PlayMusic (music, -1);
global_sdl_mix_init = true;

static SDL_Release ____ {music};
}// Init_SDL

H3R_NAMESPACE

SDLWindow::SDLWindow(int, char **)
: OSWindow (0, nullptr),
_main_window_background{Game::GetResource ("GamSelBk.pcx")}
{
if (! global_sdl_init) Init_SDL ();
}

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

void SDLWindow::Render()
{
// 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);//TODO timing
}

void SDLWindow::ProcessMessages()
{
// SDL_PumpEvents ();
if (_q) return;
while (SDL_PollEvent (&_e) != 0) {
if (_q) continue;
if (SDL_QUIT == _e.type) {
OnClose (_q);
if (_q) Close ();
}
if (_q) continue; //TODO should I?

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_WINDOWEVENT_EXPOSED == _e.window.event)
Render ();
}
if (SDL_KEYUP == _e.type && SDL_SCANCODE_Q == _e.key.keysym.scancode) {
_q = true;
OnClose (_q);
if (_q) Close ();
}
}
}// ProcessMessages

NAMESPACE_H3R

0 comments on commit a9f0f4f

Please sign in to comment.