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

Commit

Permalink
window: bridge; heres why
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 26, 2023
1 parent d55f836 commit 5ae0d5a
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 82 deletions.
30 changes: 3 additions & 27 deletions os/ui/gl/sdl/h3r_sdlwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,16 @@ SDLWindow::SDLWindow(int, char **)
if (! global_sdl_init) Init_SDL ();
}

SDLWindow::~SDLWindow()
{
glDeleteBuffers (1, &_vbo), glDeleteTextures (1, &_tex);
}
SDLWindow::~SDLWindow() {}

void SDLWindow::Render()
{
if (! _gc) return;

glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
glScalef (_w, _h, 1);

glVertexPointer (2, GL_FLOAT, 4*sizeof(GLfloat), (void *)(0));
glTexCoordPointer (
2, GL_FLOAT, 4*sizeof(GLfloat), (void *)(2*sizeof(GLfloat)));
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);

SDL_Delay (16);//TODO timing

OnRender ();
SDL_GL_SwapWindow (_window);
}

void SDLWindow::Resized()
{
if (! _h || ! _w) return;
glViewport (0, 0, _w, _h);
glMatrixMode (GL_PROJECTION), glLoadIdentity ();
// Its a 2D game.
glOrtho (.0f, _w, _h, .0f, .0f, 1.f);
// www.opengl.org/archives/resources/faq/technical/transformations.htm
glTranslatef (.375f, .375f, -.2f);
glMatrixMode (GL_MODELVIEW), glLoadIdentity ();
}
void SDLWindow::Resized() { OnResize (_w, _h); }

void SDLWindow::ProcessMessages()
{
Expand Down
49 changes: 4 additions & 45 deletions os/ui/gl/sdl/h3r_sdlwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "h3r_oswindow.h"
#include <SDL.h>
#include <SDL_opengl.h>
#include "h3r_game.h"
#include "h3r_resdecoder.h"

H3R_NAMESPACE

Expand All @@ -62,71 +60,32 @@ class SDLWindow : public OSWindow
private bool _q {false};
private bool _visible {false};

// Open GL state
GLuint _tex, _vbo;

protected inline virtual void Hide() override {}

protected inline virtual void Show() override
{
// 2.0 should be enough for a proof of concept
//TODO request this from the abstraction
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0);

_window = SDL_CreateWindow ("h3r",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _w, _h,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (! _window) {
H3R_NS::Log::Info (H3R_NS::String::Format (
H3R_NS::Log::Err (H3R_NS::String::Format (
"SDL_CreateWindow error: %s" EOL, SDL_GetError ()));
return;
}

_gc = SDL_GL_CreateContext (_window);
if (! _gc) {
H3R_NS::Log::Info (H3R_NS::String::Format (
H3R_NS::Log::Err (H3R_NS::String::Format (
"SDL_GL_CreateContext error: %s" EOL, SDL_GetError ()));
return;
}

glDisable (GL_COLOR_MATERIAL);
glEnable (GL_TEXTURE_2D);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable (GL_CULL_FACE), glCullFace (GL_BACK);
glEnable (GL_VERTEX_ARRAY); glEnable (GL_TEXTURE_COORD_ARRAY);
glClearColor (.0f, .0f, .0f, 1.f);
glDisable (GL_DEPTH_TEST);
glDisable (GL_DITHER);
glDisable (GL_BLEND);
glDisable (GL_LIGHTING);
glDisable (GL_FOG);
glDisable (GL_MULTISAMPLE);
glShadeModel (GL_FLAT);

glGenTextures (1, &_tex);
Pcx main_window_background {Game::GetResource ("GamSelBk.pcx")};
var byte_arr_ptr = main_window_background.RGB ();
if (! byte_arr_ptr || byte_arr_ptr->Empty ()) {
H3R_NS::Log::Info ("Failed to load GamSelBk.pcx");
return;
}
glBindTexture (GL_TEXTURE_2D, _tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexImage2D (GL_TEXTURE_2D, 0, /*GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,*/
GL_RGBA,
main_window_background.Width (),
main_window_background.Height (),
0, GL_RGB, GL_UNSIGNED_BYTE, byte_arr_ptr->operator byte * ());

GLfloat v[16] {0,0,0,0, 0,1,0,1, 1,0,1,0, 1,1,1,1};
glGenBuffers (1, &_vbo);
glBindBuffer (GL_ARRAY_BUFFER, _vbo),
glBufferData (GL_ARRAY_BUFFER, 16*sizeof(GLfloat), v, GL_STATIC_DRAW);
OnShow ();

Resized ();
Render ();//TODO SDL_WINDOWEVENT_EXPOSED gets lost sometimes?
Expand Down
43 changes: 34 additions & 9 deletions os/ui/h3r_oswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,25 @@ class OSWindow : public IWindow
public: virtual ~OSWindow() {}

// handle event forwarding
public: struct NoWindow final : IWindow // avoid "if"
{
inline void OnKeyDown(const EventArgs &) override {}
inline void OnKeyUp(const EventArgs &) override {}
inline void OnMouseMove(const EventArgs &) override {}
inline void OnMouseDown(const EventArgs &) override {}
inline void OnMouseUp(const EventArgs &) override {}
inline void OnShow() override {}
inline void OnHide() override {}
inline void OnClose(bool &) override {}
inline void OnRender() override {}
inline void OnResize(int, int) override {}
};
private: IWindow * _eh {};
public: virtual void SetEventHandler(IWindow * w) { _eh = w; }
public: inline virtual void SetEventHandler(IWindow * w)
{
static NoWindow n;
_eh = nullptr == w ? &n : w;
}

// Why are these virtual? Because you might want to create a monitoring
// window where you get notified with these, and forward them afterwards;
Expand All @@ -64,35 +81,43 @@ class OSWindow : public IWindow
// observer shall not get notified.
protected: inline virtual void OnKeyDown(const EventArgs & e) override
{
if (_eh) _eh->OnKeyDown (e);
_eh->OnKeyDown (e);
}
protected: inline virtual void OnKeyUp(const EventArgs & e) override
{
if (_eh) _eh->OnKeyUp (e);
_eh->OnKeyUp (e);
}
protected: inline virtual void OnMouseMove(const EventArgs & e) override
{
if (_eh) _eh->OnMouseMove (e);
_eh->OnMouseMove (e);
}
protected: inline virtual void OnMouseDown(const EventArgs & e) override
{
if (_eh) _eh->OnMouseDown (e);
_eh->OnMouseDown (e);
}
protected: inline virtual void OnMouseUp(const EventArgs & e) override
{
if (_eh) _eh->OnMouseUp (e);
_eh->OnMouseUp (e);
}
protected: inline virtual void OnShow() override
{
if (_eh) _eh->OnShow ();
_eh->OnShow ();
}
protected: inline virtual void OnHide() override
{
if (_eh) _eh->OnHide ();
_eh->OnHide ();
}
protected: inline virtual void OnClose(bool & cancel) override
{
if (_eh) _eh->OnClose (cancel);
_eh->OnClose (cancel);
}
protected: inline virtual void OnRender() override
{
_eh->OnRender ();
}
protected: inline virtual void OnResize(int w, int h) override
{
_eh->OnResize (w, h);
}
};// OSWindow

Expand Down
2 changes: 2 additions & 0 deletions ui/h3r_iwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct IWindow
virtual void OnHide() {H3R_NOT_IMPLEMENTED_EXC}
// "bool &" - allow the user to cancel the closing.
virtual void OnClose(bool &) {H3R_NOT_IMPLEMENTED_EXC}
virtual void OnRender() {H3R_NOT_IMPLEMENTED_EXC}
virtual void OnResize(int, int) {H3R_NOT_IMPLEMENTED_EXC}

// Don't forget to '#include < new >'.
// Convenience method.
Expand Down
115 changes: 115 additions & 0 deletions ui/h3r_mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2021-2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#include "h3r_mainwindow.h"
#include "h3r_game.h"
#include "h3r_resdecoder.h"
#include "h3r_thread.h"

H3R_NAMESPACE

MainWindow::~MainWindow()
{
glDeleteBuffers (1, &_vbo), glDeleteTextures (1, &_tex);
}

void MainWindow::OnShow()
{
glDisable (GL_COLOR_MATERIAL);
glEnable (GL_TEXTURE_2D);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable (GL_CULL_FACE), glCullFace (GL_BACK);
glEnable (GL_VERTEX_ARRAY); glEnable (GL_TEXTURE_COORD_ARRAY);
glClearColor (.0f, .0f, .0f, 1.f);
glDisable (GL_DEPTH_TEST);
glDisable (GL_DITHER);
glDisable (GL_BLEND);
glDisable (GL_LIGHTING);
glDisable (GL_FOG);
glDisable (GL_MULTISAMPLE);
glShadeModel (GL_FLAT);

glGenTextures (1, &_tex);
Pcx main_window_background {Game::GetResource ("GamSelBk.pcx")};
var byte_arr_ptr = main_window_background.RGB ();
if (! byte_arr_ptr || byte_arr_ptr->Empty ()) {
H3R_NS::Log::Err ("Failed to load GamSelBk.pcx");
return;
}
glBindTexture (GL_TEXTURE_2D, _tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexImage2D (GL_TEXTURE_2D, 0, /*GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,*/
GL_RGBA,
main_window_background.Width (),
main_window_background.Height (),
0, GL_RGB, GL_UNSIGNED_BYTE, byte_arr_ptr->operator byte * ());

GLfloat v[16] {0,0,0,0, 0,1,0,1, 1,0,1,0, 1,1,1,1};
glGenBuffers (1, &_vbo);
glBindBuffer (GL_ARRAY_BUFFER, _vbo),
glBufferData (GL_ARRAY_BUFFER, 16*sizeof(GLfloat), v, GL_STATIC_DRAW);

OS::Thread::Sleep (16);//TODO timing
}

void MainWindow::OnRender()
{
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
glScalef (_w, _h, 1);

glVertexPointer (2, GL_FLOAT, 4*sizeof(GLfloat), (void *)(0));
glTexCoordPointer (
2, GL_FLOAT, 4*sizeof(GLfloat), (void *)(2*sizeof(GLfloat)));
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
}

void MainWindow::OnResize(int w, int h)
{
if (! w || ! h) return;
glViewport (0, 0, _w = w, _h = h);
glMatrixMode (GL_PROJECTION), glLoadIdentity ();
// Its a 2D game.
glOrtho (.0f, _w, _h, .0f, .0f, 1.f);
// www.opengl.org/archives/resources/faq/technical/transformations.htm
glTranslatef (.375f, .375f, -.2f);
glMatrixMode (GL_MODELVIEW), glLoadIdentity ();
}

NAMESPACE_H3R
12 changes: 11 additions & 1 deletion ui/h3r_mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "h3r.h"
#include "h3r_window.h"
#include "GL/gl.h"

H3R_NAMESPACE

// Finally: the main form
// MainWindowGL
class MainWindow : public Window
{
#define public public:
#define private private:
#define protected protected:

// Open GL state
private GLuint _tex, _vbo;
private int _w {800}, _h{600};

public MainWindow(OSWindow * actual_window) : Window{actual_window} {}
public ~MainWindow() override;

protected virtual void OnShow() override;
protected virtual void OnRender() override;
protected virtual void OnResize(int w, int h) override;
};

#undef public
Expand Down
2 changes: 2 additions & 0 deletions ui/h3r_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,7 @@ void Window::OnShow() { _visible = true; }
void Window::OnHide() { _visible = false; }
// Close by default; the base window has no idea how to ask.
void Window::OnClose(bool &) { _closed = true; }
void Window::OnRender() {}
void Window::OnResize(int, int) {}

NAMESPACE_H3R
2 changes: 2 additions & 0 deletions ui/h3r_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class Window : public IWindow
IW protected virtual void OnShow() override;
IW protected virtual void OnHide() override;
IW protected virtual void OnClose(bool &) override;
IW protected virtual void OnRender() override;
IW protected virtual void OnResize(int, int) override;
};

#undef public
Expand Down

0 comments on commit 5ae0d5a

Please sign in to comment.