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

Commit

Permalink
one: main window using resmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 25, 2023
1 parent 61fd7d8 commit cb0c013
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 6 deletions.
145 changes: 145 additions & 0 deletions game/h3r_resdecoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**** 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 ****/

// A means to abstract away game-specific bitmap from render-specific one.
// E.g. istead of pcx_2_sdl(), etc.: pcx_2_RGBA_buf().

#ifndef _H3R_RESDECODER_H_
#define _H3R_RESDECODER_H_

#include "h3r.h"
#include "h3r_array.h"
#include "h3r_log.h"
#include "h3r_stream.h"
#include "h3r_string.h"

H3R_NAMESPACE

// If the returned buffer is empty, see the log for errors.
class Decoder
{
public: Decoder() {}
public: virtual ~Decoder() {}
public: virtual Array<byte> * RGBA() { return nullptr; }
public: virtual Array<byte> * RGB() { return nullptr; }
public: static int const MAX_SIZE {16384};

public: virtual int Width () { return 0; }
public: virtual int Height () { return 0; }
};

class Pcx final : public Decoder
{
private: Stream * _s;
private: Array<byte> _rgba {};
private: Array<byte> _rgb {};
private: int _w {};
private: int _h {};
public: Pcx(Stream * stream) : Decoder{}, _s{stream}
{
if (nullptr == stream)
Log::Info ("PCX: no stream " EOL);
}
public: ~Pcx() override {}
public: inline Array<byte> * RGBA() override
{
if (! _rgba.Empty ()) return &_rgba;
return Decode (_rgba, 4);
}
public: inline Array<byte> * RGB() override
{
if (! _rgb.Empty ()) return &_rgb;
return Decode (_rgb, 3);
}
public: int Width () override { return _w; }
public: int Height () override { return _h; }
private: inline Array<byte> * Decode(Array<byte> & buf, int u8_num)
{
H3R_ENSURE(3 == u8_num || 4 == u8_num, "Can't help you")
if (nullptr == _s) return &buf;
if (! *_s) return &buf;
int size, fmt;
var & s = _s->Reset ();
Stream::Read (s, &size).Read (s, &_w).Read (s, &_h);
if (_w <= 0 || _w >= Decoder::MAX_SIZE) {
Log::Err (String::Format ("PCX: Wrong width: %d" EOL, _w));
_w = _h = 0;
return &buf;
}
if (_h <= 0 || _h >= Decoder::MAX_SIZE) {
Log::Err (String::Format ("PCX: Wrong height: %d" EOL, _h));
_w = _h = 0;
return &buf;
}
fmt = size / (_w * _h);
if (1 != fmt && 3 != fmt) {
Log::Err (String::Format ("PCX: Unknwon format: %d" EOL, fmt));
_w = _h = 0;
return &buf;
}
if (_w*_h*fmt != size) {
Log::Err (String::Format ("PCX: Wrong size: %d" EOL, size));
_w = _h = 0;
return &buf;
}
buf.Resize (u8_num*_w*_h); // A, if present, is 0
byte * b = buf, * e = b + buf.Length ();
if (1 == fmt) {
Array<byte> pal {3*256};
byte * p = pal;
s.Seek (size);
Stream::Read (s, p, pal.Length ());
s.Seek (-(size + pal.Length ()));
byte i;
while (b != e) {
Stream::Read (s, &i, 1);
OS::Memmove (b, p+3*i, 3);
b += u8_num;
}
}
else
while (b != e) {
// nope, something is messed up; why not?
// Stream::Read (s, b, fmt); b += u8_num;
Stream::Read (s, b, fmt);
byte t = *b; *b = *(b+2), *(b+2) = t;
b += u8_num;
}
return &buf;
}// Decode
};// Pcx

NAMESPACE_H3R

#endif
2 changes: 1 addition & 1 deletion game/h3r_resmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ResManager : protected VFS
static_cast<int>(round (1.0*i++/all*100)),
"Looking for: " + State.Name});
// Its assignment, not comparison.
if ((State.Resource = vfs->Get (State.Name))) return;
if (nullptr != (State.Resource = vfs->Get (State.Name))) return;
}
}
public Stream * GetStream()
Expand Down
13 changes: 12 additions & 1 deletion h3r_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,20 @@ void Game::SilentLog(bool v)
_4th.Silent (v);
}

void Game::ProcessThings()
/*static*/ void Game::ProcessThings()
{
if (Game::MainWindow) Game::MainWindow->ProcessMessages ();
}

/*static*/ Stream * Game::GetResource(const String & name)
{
// Danger! Returns a Copy?!
//LATER RTFM
//var task_info = Game::RM->GetResource (name);
const var & task_info = Game::RM->GetResource (name);
while (! Game::RM->TaskComplete ())
Game::ProcessThings ();
return task_info.Resource;
}

NAMESPACE_H3R
4 changes: 3 additions & 1 deletion h3r_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Game final

public: static ResManager * RM;

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

private: class ResManagerInit final
{
private: H3R_NS::GameArchives GA {};
Expand All @@ -137,7 +139,7 @@ class Game final
//TODO there is no need for this thread (! main) to wait
// another one (! main);
// Async load the Game Archive
var task_info = RM.Load (itm.Name);
RM.Load (itm.Name);
while (! RM.TaskComplete ()) {
//LATER (messages from the IOThread)
// H3R_NS::Log::Info (H3R_NS::String::Format ("%s" EOL,
Expand Down
39 changes: 39 additions & 0 deletions os/ui/gl/sdl/h3r_sdlwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**** 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_sdlwindow.h"

H3R_NAMESPACE

NAMESPACE_H3R
19 changes: 16 additions & 3 deletions os/ui/gl/sdl/h3r_sdlwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "h3r_oswindow.h"
#include <SDL.h>
#include <SDL2/SDL_mixer.h>
#include "h3r_game.h"
#include "h3r_resdecoder.h"

H3R_NAMESPACE

Expand All @@ -53,6 +55,7 @@ class SDLWindow : public OSWindow
SDL_Window * _window {};
SDL_Surface * _screenSurface {};
SDL_Surface * _gHelloWorld {};
private: Pcx _main_window_background;
bool q {false};

protected: inline virtual void Open () override
Expand All @@ -66,11 +69,20 @@ class SDLWindow : public OSWindow
return;
}
_screenSurface = SDL_GetWindowSurface (_window);
_gHelloWorld = SDL_LoadBMP (
"../h3r_unpacked/h3/Data_H3bitmap_lod/GamSelBk.bmp");

var byte_arr_ptr = _main_window_background.RGB ();
H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_CreateRGBSurfaceFrom (w: %d, h: %d)" EOL,
_main_window_background.Width (),
_main_window_background.Height ()));
_gHelloWorld = SDL_CreateRGBSurfaceFrom (
byte_arr_ptr->operator byte * (),
_main_window_background.Width (),
_main_window_background.Height (),
24, 3 * _main_window_background.Width (), 255, 255, 255, 0);
if (! _gHelloWorld) {
H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_LoadBMP error: %s" EOL, SDL_GetError ()));
"SDL_CreateRGBSurfaceFrom error: %s" EOL, SDL_GetError ()));
return;
}

Expand Down Expand Up @@ -99,6 +111,7 @@ class SDLWindow : public OSWindow
public: void ProcessMessages() override { SDL_PumpEvents (); }

public: SDLWindow()
: _main_window_background{Game::GetResource ("GamSelBk.pcx")}
{
}

Expand Down

0 comments on commit cb0c013

Please sign in to comment.