Comments (3)
If this is done the code would be a ton of pre-processor statements.
Not true. There are multiple ways of writing platform independent code.
Preprocessor soup in the source directly is definitely one way, and you're right in pointing out that it'd be klutzy.
The way I would rather consider (as I agree, preprocessor soup sucks) is defining a kind of "trait concept", but at another weird level.
There would be some preprocessor soup when including the decl headers, although it'd be a lot less annoying, AND - most importantly,
it wouldn't directly clutter the rest of the game source with SDL2 or Win32 dependencies, only the implementation of the system and graphics contexts (which would be inside of another TU/compiler invocation)
would be affected.
This, in code form would basically be the expected interface.
Templates/ConceptsLite could be used to assert this interface somehow (as basically all platforms have a GCC version either equal to or greater to 10, C++20 could be used without issue), however you probably wouldn't even need to do that.
// Basically anything which needs some system header or whatever
struct System {
// an audio clip.
struct AudioClip {
};
static AudioClip* LoadAudioClip(const std::string_view path);
static void PlayAudioClip(AudioClip* clip);
// Other methods...
};
// This actually would need to be instantiated,
// but it handles graphics.
struct GraphicsContext {
// an interface for whatever's drawable.
struct Drawable {
virtual ~Drawable() = default;
virtual void Draw() const = 0;
};
// These are here as they would use context specific methods when drawing
struct Texture {
short GetWidth() const;
short GetHeight() const;
uint32_t* GetData() const;
// should Texture Is-A Drawable too?
// bits obvs...
};
struct Sprite : public Drawable {
Sprite(Texture* tex); // rect width/height members become { tex->GetWidth(), tex->GetHeight() } respectively
void Draw() const override;
void SetPosition(const Vector2s& pos);
private:
Texture* mTex{};
Rect<short> mRect;
};
// push an item onto the draw stack
void PushDrawStack(Drawable* drawable);
// pops the last item off the draw stack, removing it from the stack but not deleting it
// (therefore it still exists, as the managing class is what ultimately will end up deleting it).
// This function will return nullptr if no more items are in the draw stack.
Drawable* PopDrawStack();
// Clears the draw stack, rendering all items inside of it
// only called once per frame. When this returns, PresentFrame() can be called.
void RenderDrawStack();
// present the rendered frame.
void PresentFrame();
};
Anyways, the header declaring this stuff would add critical usings:
// Declare usings the rest of the game code assumes to exist
using System = Sdl2System;
using GraphicsContext = Sdl2GraphicsContext;
Those two using's would allow any code to just be written ala:
// hypothetical
void Player::Draw() {
// Push my sprite into the draw stack
g_GraphicsContext.PushDrawStack(mPlayerSprite);
}
// Yet another hypothetical one
void Player::GotCoin() {
System::PlayAudioClip(gAudioClips[AudioClips::GotCoin]);
}
from terri-fried.
If this is done the code would be a ton of pre-processor statements.
from terri-fried.
I like the way you think. I might actually give this a shot.
from terri-fried.
Related Issues (13)
- PSP code missing
- PSVITA VPK Execution: psvita C2-12828-1 HOT 4
- Source code for html
- 👏ALWAYS👏 COMMENT 👏YOUR👏CODE HOT 6
- PSP version source code. HOT 1
- type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11-narrowing] HOT 1
- [Arch Linux] segmentation fault (core dumped) HOT 2
- Suggestion: Mobile Port
- Download page needs updating
- I cant understand the instructions for the ds version
- game HOT 10
- Terri-Fried ds HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terri-fried.