Skip to content

Commit a5d7790

Browse files
authored
Merge pull request #2 from scratchcpp/refactor
Refactor the code
2 parents 6697dcf + 6752b9b commit a5d7790

File tree

161 files changed

+5080
-2871
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+5080
-2871
lines changed

CMakeLists.txt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,39 @@ find_package(utf8cpp REQUIRED)
1414

1515
add_library(scratchcpp SHARED)
1616
add_subdirectory(src)
17-
include_directories(src)
17+
include_directories(src) # TODO: Remove this line
18+
include_directories(include)
19+
20+
target_sources(scratchcpp
21+
PUBLIC
22+
include/scratchcpp/global.h
23+
include/scratchcpp/project.h
24+
include/scratchcpp/scratchconfiguration.h
25+
include/scratchcpp/iengine.h
26+
include/scratchcpp/iextension.h
27+
include/scratchcpp/iblocksection.h
28+
include/scratchcpp/asset.h
29+
include/scratchcpp/costume.h
30+
include/scratchcpp/sound.h
31+
include/scratchcpp/value.h
32+
include/scratchcpp/entity.h
33+
include/scratchcpp/variable.h
34+
include/scratchcpp/list.h
35+
include/scratchcpp/inputvalue.h
36+
include/scratchcpp/input.h
37+
include/scratchcpp/field.h
38+
include/scratchcpp/script.h
39+
include/scratchcpp/broadcast.h
40+
include/scratchcpp/compiler.h
41+
include/scratchcpp/virtualmachine.h
42+
include/scratchcpp/blockprototype.h
43+
include/scratchcpp/block.h
44+
include/scratchcpp/istagehandler.h
45+
include/scratchcpp/ispritehandler.h
46+
include/scratchcpp/target.h
47+
include/scratchcpp/stage.h
48+
include/scratchcpp/sprite.h
49+
)
1850

1951
add_library(zip SHARED
2052
${ZIP_SRC}/zip.c
@@ -23,6 +55,8 @@ add_library(zip SHARED
2355
)
2456
include_directories(thirdparty/zip/src)
2557

58+
include_directories(thirdparty/spimpl)
59+
2660
target_link_libraries(scratchcpp PRIVATE nlohmann_json::nlohmann_json)
2761
target_link_libraries(scratchcpp PRIVATE utf8cpp)
2862
target_link_libraries(scratchcpp PRIVATE zip)

Doxyfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ REPEAT_BRIEF = YES
128128
# specifies, contains, represents, a, an and the.
129129

130130
ABBREVIATE_BRIEF = "The $name class" \
131+
"The $name interface" \
131132
"The $name widget" \
132133
"The $name file" \
133134
is \
@@ -180,7 +181,7 @@ STRIP_FROM_PATH =
180181
# specify the list of include paths that are normally passed to the compiler
181182
# using the -I flag.
182183

183-
STRIP_FROM_INC_PATH = src
184+
STRIP_FROM_INC_PATH = include
184185

185186
# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but
186187
# less readable) file names. This can be useful is your file systems doesn't
@@ -865,7 +866,7 @@ WARN_LOGFILE =
865866
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
866867
# Note: If this tag is empty the current directory is searched.
867868

868-
INPUT = src \
869+
INPUT = include \
869870
docs
870871

871872
# This tag can be used to specify the character encoding of the source files
@@ -954,7 +955,8 @@ RECURSIVE = YES
954955
# Note that relative paths are relative to the directory from which doxygen is
955956
# run.
956957

957-
EXCLUDE = docs/theme
958+
EXCLUDE = docs/theme \
959+
include/scratchcpp/spimpl.h
958960

959961
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
960962
# directories that are symbolic links (a Unix file system feature) are excluded

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ We're working on the documentation, it'll be available soon.
9595

9696
A minimal CLI program for running Scratch projects:
9797
```cpp
98-
#include <scratchproject.h>
98+
#include <scratchcpp/project.h>
9999

100100
int main(int argc, char **argv) {
101-
libscratchcpp::ScratchProject p("/path/to/project.sb3");
101+
libscratchcpp::Project p("/path/to/project.sb3");
102102
bool ret = p.load();
103103
if (!ret)
104104
return 1;

docs/Block sections.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ void MySection::compileHelloWorld(Compiler *compiler) {
7474
}
7575
```
7676
77-
The value of the input can be read during runtime using the \link libscratchcpp::VirtualMachine::getArg() getArg() \endlink function:
77+
The value of the input can be read during runtime using the \link libscratchcpp::VirtualMachine::getInput() getInput() \endlink function:
7878
```cpp
7979
unsigned int MySection::helloWorld(VirtualMachine *vm) {
80-
std::cout << "Hello, " << vm->getArg(0, 1)->toString() << "!" << std::endl;
80+
std::cout << "Hello, " << vm->getInput(0, 1)->toString() << "!" << std::endl;
8181
return 1;
8282
}
8383
```
8484
\note The order of the inputs is the same as in the compile function. Do not use the `Inputs` enumeration in runtime functions.
8585

8686
```cpp
87-
vm->getArg(0, 1)
87+
vm->getInput(0, 1)
8888
```
8989
The first argument is the index of the input and the second argument is the amount of inputs.
9090
\note Make sure to return the amount of inputs in the `helloWorld` function.

docs/Extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using namespace libscratchcpp;
2020
### Registering block sections
2121
Block sections can be registered by overriding the \link libscratchcpp::IExtension::registerSections() registerSections() \endlink function:
2222
```cpp
23-
void MyExtension::registerSections(Engine *engine) {
23+
void MyExtension::registerSections(IEngine *engine) {
2424
engine->registerSection(std::make_shared<MySection>());
2525
}
2626
```

docs/Getting started.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
\page gettingStarted Getting started
22

33
libscratchcpp doesn't have any GUI support, but sprites and the stage can be easily implemented using
4-
the \link libscratchcpp::IScratchStage IScratchStage \endlink and
5-
\link libscratchcpp::IScratchTarget IScratchTarget \endlink interfaces.
4+
the \link libscratchcpp::IStageHandler IStageHandler \endlink and
5+
\link libscratchcpp::ISpriteHandler ISpriteHandler \endlink interfaces.
66

77
They can be subclassed for listening to events such as when the X and Y coordinates
88
of a sprite change.
99

1010
## CLI-only example
1111
```cpp
12-
#include <scratchproject.h>
12+
#include <scratchcpp/project.h>
1313

1414
int main(int argc, char **argv) {
15-
libscratchcpp::ScratchProject p("/path/to/project.sb3");
15+
libscratchcpp::Project p("/path/to/project.sb3");
1616
bool ret = p.load();
1717
if (!ret)
1818
return 1;
@@ -21,9 +21,9 @@ int main(int argc, char **argv) {
2121
return 0;
2222
}
2323
```
24-
The \link libscratchcpp::ScratchProject::run() run() \endlink method runs an event loop which stops after all scripts finish.
24+
The \link libscratchcpp::Project::run() run() \endlink method runs an event loop which stops after all scripts finish.
2525
26-
For CLI project players, using \link libscratchcpp::ScratchProject::run() run() \endlink is enough. If you are developing
26+
For CLI project players, using \link libscratchcpp::Project::run() run() \endlink is enough. If you are developing
2727
a GUI project player and need to receive input events such as key presses, you'll need to implement your own event loop
2828
or use the one provided by the GUI framework.
2929
@@ -33,18 +33,19 @@ Qt provides an event loop which can be easily used to run frames with a specific
3333
For example, a `Player` class which **inherits from QObject** can use a timer:
3434
```cpp
3535
#include <QObject>
36-
#include <scratchproject.h>
36+
#include <scratchcpp/project.h>
3737
3838
class Player : public QObject {
3939
Q_OBJECT
4040
public:
4141
Player(QObject *parent = nullptr);
42+
Player(const Player &) = delete; // Project is not copyable
4243
4344
protected:
4445
void timerEvent(QTimerEvent *event) override;
4546
4647
private:
47-
libscratchcpp::ScratchProject m_proj;
48+
libscratchcpp::Project m_proj;
4849
}
4950
5051
Player::Player(QObject *parent) {

docs/Virtual machine.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ OP_LOOP_END
5353
### Repeat until
5454
Scratch has a while loop, but until is used more frequently, so the VM doesn't support the while loop.
5555

56-
The repeat until loop can be implemented using \link libscratchcpp::vm::OP_REPEAT_UNTIL OP_REPEAT_UNTIL \endlink
56+
The repeat until loop can be implemented using \link libscratchcpp::vm::OP_UNTIL_LOOP OP_UNTIL_LOOP \endlink
5757
and \link libscratchcpp::vm::OP_BEGIN_UNTIL_LOOP OP_BEGIN_UNTIL_LOOP \endlink.
5858
```
59-
OP_REPEAT_UNTIL
59+
OP_UNTIL_LOOP
6060
(evaluate the condition here)
6161
OP_BEGIN_UNTIL_LOOP
6262
...
6363
OP_LOOP_END
6464
```
65-
\note The condition is evaluated after \link libscratchcpp::vm::OP_REPEAT_UNTIL OP_REPEAT_UNTIL \endlink and the loop
65+
\note The condition is evaluated after \link libscratchcpp::vm::OP_UNTIL_LOOP OP_UNTIL_LOOP \endlink and the loop
6666
starts with \link libscratchcpp::vm::OP_BEGIN_UNTIL_LOOP OP_BEGIN_UNTIL_LOOP \endlink.
6767

6868
### Forever
@@ -75,7 +75,7 @@ OP_LOOP_END
7575

7676
This is equivalent to:
7777
```
78-
OP_REPEAT_UNTIL
78+
OP_UNTIL_LOOP
7979
OP_NULL
8080
OP_BEGIN_UNTIL_LOOP
8181
...
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
#pragma once
44

5-
#include "../libscratchcpp_global.h"
65
#include <string>
6+
#include "spimpl.h"
7+
8+
#include "global.h"
79

810
namespace libscratchcpp
911
{
1012

13+
class AssetPrivate;
14+
1115
/*! \brief The Asset class represents a Scratch asset, for example a Costume or a Sound. */
1216
class LIBSCRATCHCPP_EXPORT Asset
1317
{
1418
public:
1519
Asset(std::string name, std::string id, std::string format);
1620

17-
std::string assetId() const;
21+
const std::string &assetId() const;
1822

19-
std::string name() const;
23+
const std::string &name() const;
2024

21-
std::string md5ext() const;
25+
const std::string &md5ext() const;
2226

23-
std::string dataFormat() const;
27+
const std::string &dataFormat() const;
2428

2529
private:
26-
std::string m_assetId;
27-
std::string m_name;
28-
std::string m_dataFormat;
30+
spimpl::impl_ptr<AssetPrivate> impl;
2931
};
3032

3133
} // namespace libscratchcpp
Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
#pragma once
44

5-
#include "../engine/global.h"
6-
#include "field.h"
7-
#include "ientity.h"
8-
#include "input.h"
5+
#include "entity.h"
96
#include "blockprototype.h"
10-
#include <memory>
11-
#include <unordered_map>
12-
#include <string>
137

148
namespace libscratchcpp
159
{
1610

17-
class LIBSCRATCHCPP_EXPORT Engine;
18-
class LIBSCRATCHCPP_EXPORT Target;
11+
class IEngine;
12+
class Target;
13+
class Input;
14+
class Field;
15+
class BlockPrivate;
1916

2017
/*! \brief The Block class represents a Scratch block. */
21-
class LIBSCRATCHCPP_EXPORT Block : public IEntity
18+
class LIBSCRATCHCPP_EXPORT Block : public Entity
2219
{
2320
public:
24-
Block(std::string id, std::string opcode);
21+
Block(const std::string &id, const std::string &opcode);
2522
Block(const Block &) = delete;
2623

2724
void compile(Compiler *compiler);
@@ -58,7 +55,7 @@ class LIBSCRATCHCPP_EXPORT Block : public IEntity
5855
bool topLevel() const;
5956
void setTopLevel(bool newTopLevel);
6057

61-
void setEngine(Engine *newEngine);
58+
void setEngine(IEngine *newEngine);
6259

6360
void setTarget(Target *newTarget);
6461

@@ -71,22 +68,7 @@ class LIBSCRATCHCPP_EXPORT Block : public IEntity
7168
BlockPrototype *mutationPrototype();
7269

7370
private:
74-
std::string m_opcode;
75-
BlockComp m_compileFunction = nullptr;
76-
std::shared_ptr<Block> m_next = nullptr;
77-
std::string m_nextId;
78-
std::shared_ptr<Block> m_parent = nullptr;
79-
std::string m_parentId;
80-
std::vector<std::shared_ptr<Input>> m_inputs;
81-
std::unordered_map<int, Input *> m_inputMap;
82-
std::vector<std::shared_ptr<Field>> m_fields;
83-
std::unordered_map<int, Field *> m_fieldMap;
84-
bool m_shadow = false;
85-
bool m_topLevel = false;
86-
Engine *m_engine = nullptr;
87-
Target *m_target = nullptr;
88-
BlockPrototype m_mutationPrototype;
89-
bool m_mutationHasNext = true;
71+
spimpl::unique_impl_ptr<BlockPrivate> impl;
9072
};
9173

9274
} // namespace libscratchcpp
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
#pragma once
44

5-
#include "value.h"
65
#include <vector>
76

7+
#include "spimpl.h"
8+
#include "value.h"
9+
810
namespace libscratchcpp
911
{
1012

13+
class BlockPrototypePrivate;
14+
1115
/*! \brief The BlockPrototype class represents the prototype of a custom block. */
1216
class LIBSCRATCHCPP_EXPORT BlockPrototype
1317
{
@@ -38,12 +42,7 @@ class LIBSCRATCHCPP_EXPORT BlockPrototype
3842
void setWarp(bool newWarp);
3943

4044
private:
41-
std::string m_procCode;
42-
std::vector<std::string> m_argumentIds;
43-
std::vector<std::string> m_argumentNames;
44-
std::vector<Value> m_argumentDefaults;
45-
std::vector<ArgType> m_argumentTypes;
46-
bool m_warp = false;
45+
spimpl::impl_ptr<BlockPrototypePrivate> impl;
4746
};
4847

4948
} // namespace libscratchcpp

0 commit comments

Comments
 (0)