Skip to content

Commit bd6ac3e

Browse files
committed
Added Default SceneError
1 parent 96bfe85 commit bd6ac3e

9 files changed

Lines changed: 179 additions & 57 deletions

File tree

Bindings/Engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <wrenbind17/wrenbind17.hpp>
44

55
namespace Wren = wrenbind17;
6-
6+
77
struct EngineCls {};
88

99
void BindEngine(Wren::VM & VM) {

docs/Documentation.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
---
2-
title: API Documentation
2+
title: Documentation
33
layout: default
44
nav_order: 3
55
---
66

77
# API Documentation
88

9-
Cube2D Engine just binds or recreate things from [raylib], [raymath], [Cube2D Framework] so there is no need for docs of my own and as [raylib] says, you can just learn things just by looking at the cheatsheet, but in my personal opinion, switching between cheetsheet and code editor is kinda pain so just include [raylib.h], [raymath], [Cube2D.hpp] in your project and your code editor most likely supports better file switch compared to what you would do if those files were not included.
9+
Cube2D Engine just binds or recreate things from [raylib], [raymath], [Cube2D Framework], and as [raylib] says, you can just learn things just learn things by looking at their cheetsheet, but in my personal opinion, switching between cheetsheet and code editor is kinda pain so just include [raylib.h], [raymath], [Cube2D.hpp] in your project and your code editor most likely supports better file switch compared to what you would do if those files were not included.
10+
11+
Now as for the Engine itself, you just gotta see the tutorials.
1012

1113
## How To Use Enums & Defines?
12-
[raylib] defines the following enums
14+
The following enums are few of enums defined by [raylib].
1315
```c
1416
typedef enum {
1517
MOUSE_BUTTON_LEFT = 0, // Mouse button left
@@ -55,8 +57,8 @@ You just need to learn keywords, like `Engine` from "Cube2D"
5557

5658
## What Cube2D Engine Doesn't Bind?
5759

58-
Q: Wait, what? I thought Cube2D Engine binds everything from those files.
59-
A: Nuh uh, not currently at least.
60+
- Q: Wait, what? I thought Cube2D Engine binds everything from those files.
61+
- A: Nuh uh, not currently at least.
6062

6163
Cube2D Engine also does not yet bind classes which have members like `float v[3]`, you know, the c style array thing. but they will be bind-ed later.
6264

@@ -69,11 +71,11 @@ Lets start with the simplest, [Cube2D Framework]
6971
#### `Scene`
7072
`Scene` is a `class` for scene management which heavily relies on inheritance but for some reason [wren] doesn't support inheritance from `foreign` classes, `foreign` classes refer to classes bind-ed by the engine in our case.
7173

72-
Q: But according to home page's example, we were totally doing inheritance of `Scene`
73-
A: In order to make it work I had to create equivalent of `Scene` `class` in [wren] to support inheritance which is contained in the `Cube2D` module you `import` in your wren file.
74+
- Q: But according to home page's example, we were totally doing inheritance of `Scene`
75+
- A: In order to make it work I had to create equivalent of `Scene` `class` in [wren] to support inheritance which is contained in the `Cube2D` module you `import` in your wren file.
7476

75-
Q: So here will be documentation for equivalent of `Scene` `class` ?
76-
A: Nope, it works exactly the same.
77+
- Q: So here will be documentation for equivalent of `Scene` `class` ?
78+
- A: Nope, it works exactly the same.
7779

7880
#### `Rect`
7981

@@ -83,8 +85,8 @@ A: Nope, it works exactly the same.
8385
`Rect` is a `class` for easier rectangular manipulation which you would also like to add inheritance to create stuff like entity, tile, player, enemy, etc. but `Rect` is a little more hard `class` to be recreated in wren, so I created bind-ed `Rect` `class` from c++ as `OrignalRect` in wren and created a wrapper `class` in purely in wren which calls `OrignalRect` behind the scenes
8486

8587

86-
Q: So why we were using `.Base` for `Engine.WASDMovement`?
87-
A: `Engine.WASDMovement` takes `OrignalRect` aka C++ `Rect` to move it but `Rect` in [wren] is a [wren] `class` so it throws a bad cast error so I added a `.Base` getter to get the real deal which you can pass onto `Engine.WASDMovement`.
88+
- Q: So why we were using `.Base` for `Engine.WASDMovement`?
89+
- A: `Engine.WASDMovement` takes `OrignalRect` aka C++ `Rect` to move it but `Rect` in [wren] is a [wren] `class` so it throws a bad cast error so I added a `.Base` getter to get the real deal which you can pass onto `Engine.WASDMovement`.
8890

8991
#### `Init` & `Close`
9092
Those two are intentionally not bind-ed as they are handled by the engine.
@@ -93,11 +95,11 @@ Those two are intentionally not bind-ed as they are handled by the engine.
9395

9496
The next simplest one is [raymath]
9597

96-
Q: How do I use [raymath]'s `PI` and stuff, it doesn't follow the syntax shown in [How To Use Enums & Defines]
97-
A: `import "raymath" for PI, EPSILON, DEG2RAD, RAD2DEG`
98+
- Q: How do I use [raymath]'s `PI` and stuff, it doesn't follow the syntax shown in [How To Use Enums & Defines]
99+
- A: `import "raymath" for PI, EPSILON, DEG2RAD, RAD2DEG`
98100

99-
Q: What about `MatrixToFloat` and `Vector3ToFloat`?
100-
A: Later.
101+
- Q: What about `MatrixToFloat` and `Vector3ToFloat`?
102+
- A: Later.
101103

102104
#### Things that [raylib] defines
103105
It would be real dumb to rebind things that [raylib] also defines so `import` those stuff from `raylib` module.

docs/ErrorHanding.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Error Handling
3+
layout: default
4+
nav_order: 5
5+
---
6+
7+
# Error Handling
8+
9+
As you saw in [Tutorials/Configuration], `Game.wren`'s `Path` variable has a key named "SceneError" which is used for error handling but how exactly?
10+
11+
Don't worry, It will be real simple.
12+
13+
Here is a example
14+
```js
15+
import "raylib" for RL, COLOR
16+
import "Cube2D" for Scene
17+
18+
class SceneError is Scene {
19+
construct new() {
20+
_Error = "NONE"
21+
super()
22+
}
23+
24+
SetError(Error) {
25+
_Error = Error
26+
}
27+
28+
Update() {
29+
30+
}
31+
32+
Draw() {
33+
if(_Error != "NONE") RL.DrawText(_Error, 32, 32, 16, COLOR["RED"])
34+
}
35+
}
36+
37+
var Instance = SceneError.new()
38+
```
39+
**The above code has been tested with correct paths.**
40+
41+
You just need a `var` named `Instance` in your error scene file will engine use to throw error and a special method in your scene named `SetError` with signature `_` i.e `SetError(Error)` where you can save the error for displaying later on using Draw() method and as of course you don't need to call `.Run()` cuz the engine does that when the time is right.

docs/Tutorials/ConfigSave Files.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Config/Save Files
33
layout: default
44
parent: Tutorials
5-
nav_order: 4
5+
nav_order: 5
66
---
77

88
# Config/Save Files
@@ -59,7 +59,7 @@ First, here's a .json file
5959

6060
Second, Here's a .wren file
6161
```js
62-
// File: SceneSomething.wren
62+
// File: SceneExample.wren
6363
import "Cube2D" for Engine, Scene, Rect
6464
import "raylib" for RL, COLOR
6565
import "path/to/json.wren" for JSON

docs/Tutorials/Creating A Scene.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Creating A Scene
3+
layout: default
4+
parent: Tutorials
5+
nav_order: 3
6+
---
7+
8+
# Creating A Scene
9+
10+
Lets create a simple scene where you can move around player.
11+
12+
- Create a folder named "Scenes" in your desired `Scripts` folder (optional)
13+
- Create a file for your scene with your desired name ( I Prefer SceneExample )
14+
- `import "Cube2D" for Scene`
15+
- Create a class as following
16+
```js
17+
import "Cube2D" for Scene
18+
19+
20+
class SceneExample is Scene {
21+
construct new() {
22+
23+
}
24+
}
25+
```
26+
As you see we are inheriting Scene from the Cube2D module for our usage. So first of all we need a constructer for our new `SceneExample` class which you can use like `SceneExample.new()` but since we are inheriting `Scene` class we can also call `Scene` classes methods so our class is ready to be used like `SceneExample.new().Run()`.
27+
28+
- You probably wanna draw something to screen so lets draw a simple rectangle to screen for example.
29+
- Add `Draw() {}` method to your class.
30+
- `import "raylib" for COLOR`
31+
- also `import` `Rect` from `Cube2D`
32+
```js
33+
import "Cube2D" for Scene, Rect
34+
35+
class SceneExample is Scene {
36+
construct new() {
37+
_Player = Rect.new(0, 0, 32, 32) // Create a rectangle at (0, 0) with size (32, 32).
38+
_Player.Tint = COLOR["RED"] // Add a color to the rectangle.
39+
}
40+
Draw() {
41+
_Player.Draw()
42+
}
43+
}
44+
```
45+
46+
- Now You probably also wanna controll that player.
47+
- also `import` `Engine` from `Cube2D`
48+
- Add `Update() {}` method to your class.
49+
50+
```cpp
51+
import "Cube2D" for Engine, Scene, Rect
52+
import "raylib" for COLOR
53+
54+
class SceneExample is Scene {
55+
construct new() {
56+
57+
_Player = Rect.new(0, 0, 32, 32)
58+
_Player.Tint = COLOR["RED"]
59+
60+
super()
61+
}
62+
Update() {
63+
64+
var Speed = 24
65+
Engine.WASDMovement(_Player.Base, Speed)
66+
}
67+
Draw() {
68+
_Player.Draw()
69+
}
70+
}
71+
```
72+
73+
- You can also create a new scene that inherits `SceneExample` or any other scene to call methods from that.

docs/Tutorials/Initialization.md

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,16 @@ For now, lets make a simple game where you have to move player around.
1313

1414
```js
1515
// File: Main.wren
16-
import "raylib" for COLOR
16+
// import whatever you need.
17+
import "raylib" for RL, COLOR
18+
import "raymath" for RM
1719
import "Cube2D" for Engine, Scene, Rect
1820

19-
class SceneGame is Scene {
20-
construct new() {
21-
_Player = Rect.new(0, 0, 32, 32)
22-
_Player.Tint = COLOR["RED"]
23-
24-
super()
25-
}
26-
Update() {
27-
var Speed = 10
28-
// We are still too busy to look into why we use .Base here.
29-
Engine.WASDMovement(_Player.Base, Speed)
30-
}
31-
Draw() {
32-
_Player.Draw()
33-
}
34-
}
35-
36-
SceneGame.new().Run()
21+
// Sorry, Coudn't think of much here.
3722

3823
```
3924
**The above code has been tested with correct paths.**
4025
Very Minimal, but you can do a lots of initialization stuff for your game like loading config files etc.
4126
speaking of loading config files, you surely want some sort of configuration data saving file etc.
4227
And Cube2D Engine provides nothing of a sort but there's a alternative that takes no time at all to setup.
43-
We will look into that later though, cuz can't start compilacated stuff even before you know anything.
44-
45-
46-
You can also use that file as following supposing that file is located at `Scripts/Scenes/SceneGame.wren`
47-
```js
48-
// File: Main.wren
49-
import "Scenes/SceneGame.wren" for SceneGame
50-
51-
SceneGame.new().Run()
52-
53-
```
54-
**The above code has been tested with correct paths.**
55-
As you see you are importing `SceneGame` only not the whole file so any code in that file won't be ran
28+
We will look into that later though, cuz can't start compilacated stuff even before you know anything.

docs/Tutorials/Textures.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Textures
33
layout: default
44
parent: Tutorials
5-
nav_order: 3
5+
nav_order: 4
66
---
77

88
# Textures
@@ -31,4 +31,6 @@ class SceneGame is Scene {
3131
```
3232
**The above code has been tested with correct paths.**
3333

34-
I'll not be using single file approach from now cuz by now, you should know how to run that code and have a well defined project folder.
34+
I'll not be using single file approach from now cuz by now, you should know how to run that code and have a well defined project folder.
35+
36+
[Cube2D Framework]: https://github.com/mastercuber55/Cube2D-Framework

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ nav_order: 1
66

77
# Home
88

9+
![Cube2D Engine](https://raw.githubusercontent.com/mastercuber55/Cube2D-Engine/main/docs/Assets/Images/Banner.png)
10+
911
Cube2D Engine, A minimal 2D simple and easy to use game development engine that provides everything you need for game development and utilizes super fast and super simple [wren] as its scripting language.
1012

1113
## Features

main.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99

1010
namespace Wren = wrenbind17;
1111

12+
struct SceneError : Engine::Scene {
13+
14+
std::string Error;
15+
Vector2 Size;
16+
17+
SceneError(std::string Error) {
18+
this->Error = Error;
19+
Size = MeasureTextEx(GetFontDefault(), Error.c_str(), 24, 2);
20+
21+
SetWindowTitle("Cube2D - An Error Occured");
22+
SetWindowSize(Size.x + 24 / 2, Size.y + 24 / 2);
23+
}
24+
25+
void Update() {}
26+
void Draw() {
27+
DrawText(Error.c_str(), 24 / 4, 32 / 4, 24, RED);
28+
// GuiTextBox({0, 0, 32 * 5, 32}, const_cast<char*>(this->Error.c_str()), 0, false);
29+
}
30+
31+
~SceneError() {}
32+
};
33+
34+
1235
// struct SceneGen : Engine::Scene {
1336

1437
// Vector2 anchor01 = { 0, -24 };
@@ -90,7 +113,7 @@ paths Paths;
90113
bool Logging;
91114

92115
void CustomLog(int LOG_LEVEL, const char * Text, va_list Args);
93-
void InitFromConfig(Wren::VM & VM);
116+
int InitFromConfig(Wren::VM & VM);
94117

95118
void BindRaylib(Wren::VM & VM);
96119
void BindRaymath(Wren::VM & VM);
@@ -127,7 +150,7 @@ int main() {
127150
BindRaymath(VM);
128151
BindEngine(VM);
129152

130-
InitFromConfig(VM);
153+
if(InitFromConfig(VM) == EXIT_FAILURE) return EXIT_FAILURE;
131154

132155
// return SceneGen().Run();
133156

@@ -165,14 +188,18 @@ void CustomLog(int LOG_LEVEL, const char * Text, va_list Args) {
165188
char buffer[1024];
166189
vsnprintf(buffer, sizeof(buffer), Text, Args);
167190

191+
std::cout << Tag << buffer << std::endl;
168192
LogFile.is_open() ? (LogFile << Tag << buffer << std::endl) : LogStream << Tag << buffer << std::endl;
169193
}
170194

171-
void InitFromConfig(Wren::VM & VM) {
195+
int InitFromConfig(Wren::VM & VM) {
172196
// Load Configuration File
173197
if(!FileExists("Game.wren")) {
174198
TraceLog(LOG_ERROR, "[%s] was not found.", "Game.wren");
175-
return;
199+
Engine::Init(640, 480, "Cube2D - An Error Occured");
200+
SceneError("[Game.wren] was not found.").Run();
201+
Engine::Close();
202+
return EXIT_FAILURE;
176203
}
177204
VM.runFromModule("Game.wren");
178205

@@ -197,7 +224,7 @@ void InitFromConfig(Wren::VM & VM) {
197224

198225
if(!WindowGet("Title").is<std::string>()) {
199226
TraceLog(LOG_ERROR, "Window title which is a must was not provided.");
200-
return;
227+
return EXIT_FAILURE;
201228
}
202229
std::string Title = WindowGet("Title").as<std::string>();
203230
Engine::Init(Width, Height, Title);
@@ -227,4 +254,6 @@ void InitFromConfig(Wren::VM & VM) {
227254
Paths.MainScript = PathGet("MainScript").as<std::string>();
228255
Paths.SceneError = PathGet("SceneError").as<std::string>();
229256

257+
258+
return EXIT_SUCCESS;
230259
}

0 commit comments

Comments
 (0)