Codebase list surgescript / ae350e8
Import upstream version 0.5.6 Debian Janitor 1 year, 8 months ago
44 changed file(s) with 1067 addition(s) and 303 deletion(s). Raw diff Collapse all Expand all
0 # These are supported funding model platforms
1
2 github: alemart # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
3 patreon: # Replace with a single Patreon username
4 open_collective: # Replace with a single Open Collective username
5 ko_fi: # Replace with a single Ko-fi username
6 tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
7 community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
8 liberapay: # Replace with a single Liberapay username
9 issuehunt: # Replace with a single IssueHunt username
10 otechie: # Replace with a single Otechie username
11 custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
77 !.vscode/tasks.json
88 !.vscode/launch.json
99 !.vscode/extensions.json
10
11 # msvc
12 .vs/
13 out/
14 CMakeSettings.json
1015
1116 # vim
1217 # swap
2126 *~
2227
2328 # mine
24 surgescript
25 *.a
26 *.so
27 *.so.*
28 *.dll
29 *.exe
30 *.pc
31 site/
29 docs_html/
3230 build/
3331 private/
3432 CMakeFiles/
00 # Release Notes
1
2 ## 0.5.6 - September 1st, 2022
3
4 * Improved the SurgeScript CLI with a time limit option, the ability to run scripts from stdin and optional multithreading support
5 * Added Visual Studio support (Cody Licorish)
6 * Added Emscripten support
7 * Updated docs
8 * General improvements
9
10 ## 0.5.5 - January 22nd, 2021
11
12 * Added the ability to pause the SurgeScript VM
13 * Added utility macros for checking the SurgeScript version at compile time
14 * Introduced a dedicated module for keeping track of time
15 * Renamed Object.childCount to Object.__childCount
16 * Updated docs
117
218 ## 0.5.4.4 - April 16th, 2020
319
00 # ------------------------------------------------------------------------------
11 # SurgeScript
22 # A scripting language for games
3 # Copyright 2016-2020 Alexandre Martins <alemartf(at)gmail(dot)com>
3 # Copyright 2016-2022 Alexandre Martins <alemartf(at)gmail(dot)com>
44 # ------------------------------------------------------------------------------
55
66 # Project info
77 cmake_minimum_required(VERSION 3.2)
88 project(
99 surgescript
10 VERSION 0.5.4.4
10 VERSION 0.5.6
1111 LANGUAGES C
1212 )
13 include(GNUInstallDirs)
14 include(CheckLibraryExists)
15 set(PROJECT_YEARS "2016-2022")
1316
1417 # Default config
15 set(CMAKE_C_STANDARD 99)
18 set(CMAKE_C_STANDARD 11)
1619 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
1720 set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build: Debug | Release | MinSizeRel | RelWithDebInfo" FORCE)
1821 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
1922 endif()
20 if(UNIX AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
21 set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix (prepended onto install directories)" FORCE)
22 endif()
2323
2424 # User options
2525 option(WANT_SHARED "Build SurgeScript as a shared library" ON)
2626 option(WANT_STATIC "Build SurgeScript as a static library" ON)
27 option(WANT_EXECUTABLE "Build the surgescript executable" ON)
28 set(LIB_SUFFIX "" CACHE STRING "Suffix to append to 'lib' directories, e.g., '64'") # libs must be installed to "lib64" in some systems
29 set(PKGCONFIG_PATH "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/pkgconfig" CACHE PATH "Destination folder of the pkg-config (.pc) file")
27 option(WANT_EXECUTABLE "Build the SurgeScript CLI" ON)
28 option(WANT_EXECUTABLE_MULTITHREAD "Enable multithreading on the SurgeScript CLI" ON)
29 set(PKGCONFIG_PATH "pkgconfig" CACHE PATH "Destination folder of the pkg-config (.pc) file")
3030 if(UNIX)
31 set(METAINFO_PATH "/usr/share/metainfo" CACHE PATH "Destination folder of the metainfo file")
32 set(ICON_PATH "/usr/share/pixmaps" CACHE PATH "Destination folder of the icon file")
33 file(TO_CMAKE_PATH "${ICON_PATH}/surgescript.png" ICON_FILEPATH)
34 endif()
35
36 # Output folder
37 set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR})
38 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
39 set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
31 set(METAINFO_PATH "metainfo" CACHE PATH "Destination folder of the metainfo file")
32 set(ICON_PATH "pixmaps" CACHE PATH "Destination folder of the icon file")
33 endif()
34
35 # Library search
36 CHECK_LIBRARY_EXISTS(m sqrt "${CMAKE_SYSTEM_LIBRARY_PATH}" SURGESCRIPT_libm_EXISTS)
4037
4138 # Sources
4239 set(
7471 src/surgescript/runtime/tag_system.c
7572 src/surgescript/runtime/variable.c
7673 src/surgescript/runtime/vm.c
74 src/surgescript/runtime/vm_time.c
7775 src/surgescript/util/transform.c
7876 src/surgescript/util/utf8.c
7977 src/surgescript/util/util.c
8078 src/surgescript/util/xoroshiro128plus.c
81 ${CMAKE_CURRENT_BINARY_DIR}/info.c
79 ${CMAKE_BINARY_DIR}/src/surgescript/misc/info.c
8280 )
8381
8482 # Headers
102100 src/surgescript/runtime/tag_system.h
103101 src/surgescript/runtime/variable.h
104102 src/surgescript/runtime/vm.h
103 src/surgescript/runtime/vm_time.h
105104 src/surgescript/util/fasthash.h
106105 src/surgescript/util/ssarray.h
107106 src/surgescript/util/transform.h
111110 src/surgescript.h
112111 )
113112
113 # Output folder
114 set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
115
114116 # Generate files from templates
115117 function(generate_file TEMPLATE)
116 configure_file(src/surgescript/misc/${TEMPLATE}.in ${TEMPLATE} @ONLY)
118 configure_file(src/surgescript/${TEMPLATE}.in "${CMAKE_BINARY_DIR}/src/surgescript/${TEMPLATE}" @ONLY)
117119 endfunction()
118120
119121 function(generate_pc_file LINKAGE)
122124 set(LIB_LINKAGE "-static")
123125 endif()
124126 configure_file(src/surgescript/misc/surgescript.pc.in "${LIBRARY_OUTPUT_PATH}/surgescript${LIB_LINKAGE}.pc" @ONLY)
125 install(FILES "${LIBRARY_OUTPUT_PATH}/surgescript${LIB_LINKAGE}.pc" DESTINATION "${PKGCONFIG_PATH}")
127 install(FILES "${LIBRARY_OUTPUT_PATH}/surgescript${LIB_LINKAGE}.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/${PKGCONFIG_PATH}")
126128 endfunction()
127129
128 generate_file("info.c")
129
130
131 # Library
130 generate_file("misc/info.c")
131 generate_file("util/version.h")
132
133 # Build the library
132134 if(NOT WANT_SHARED AND NOT WANT_STATIC)
133135 message(FATAL_ERROR "Options WANT_SHARED and WANT_STATIC are both set to OFF. Nothing to do.")
134136 endif()
135137
136138 if(WANT_SHARED)
139 set(LIB_SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") # x.y.z: backwards compatibility
137140 message(STATUS "Will build libsurgescript")
138141 generate_pc_file("shared")
139142 add_library(surgescript SHARED ${SURGESCRIPT_SOURCES} ${SURGESCRIPT_HEADERS})
140 target_link_libraries(surgescript m)
141 set_target_properties(surgescript PROPERTIES VERSION ${PROJECT_VERSION})
142 install(TARGETS surgescript DESTINATION "lib${LIB_SUFFIX}")
143 if (SURGESCRIPT_libm_EXISTS)
144 target_link_libraries(surgescript m)
145 endif()
146 set_target_properties(surgescript PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${LIB_SOVERSION})
147 install(TARGETS surgescript DESTINATION "${CMAKE_INSTALL_LIBDIR}")
143148 endif()
144149
145150 if(WANT_STATIC)
146151 message(STATUS "Will build libsurgescript-static")
147152 generate_pc_file("static")
148153 add_library(surgescript-static STATIC ${SURGESCRIPT_SOURCES} ${SURGESCRIPT_HEADERS})
149 target_link_libraries(surgescript-static m)
154 if (SURGESCRIPT_libm_EXISTS)
155 target_link_libraries(surgescript-static m)
156 endif ()
150157 set_target_properties(surgescript-static PROPERTIES VERSION ${PROJECT_VERSION})
151 install(TARGETS surgescript-static DESTINATION "lib${LIB_SUFFIX}")
152 endif()
153
154 install(DIRECTORY src/ DESTINATION include FILES_MATCHING PATTERN "*.h") # installing the headers
155
156 # Executable
158 install(TARGETS surgescript-static DESTINATION "${CMAKE_INSTALL_LIBDIR}")
159 endif()
160
161 # Install headers
162 install(DIRECTORY src/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" FILES_MATCHING PATTERN "*.h")
163 install(DIRECTORY "${CMAKE_BINARY_DIR}/src/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" FILES_MATCHING PATTERN "*.h")
164
165 # Build the SurgeScript CLI
157166 if(WANT_EXECUTABLE)
167 message(STATUS "Will build the SurgeScript CLI")
168
158169 # Set the appropriate lib
159170 set(LIBSURGESCRIPT "surgescript")
160171 if(WANT_STATIC AND (NOT WANT_SHARED OR WIN32))
161172 set(LIBSURGESCRIPT "surgescript-static")
162173 endif()
163174
175 # Use multithreading?
176 set(LIBTHREAD "")
177 set(ENABLE_THREADS 0)
178 if(WANT_EXECUTABLE_MULTITHREAD)
179 message(STATUS "Will use multithreading on the SurgeScript CLI")
180
181 # hmmmm...
182 set(LIBTHREAD "pthread")
183 set(ENABLE_THREADS 1)
184 endif()
185
164186 # Create the executable
165 message(STATUS "Will build surgescript")
166187 add_executable(surgescript.bin src/main.c)
167 target_link_libraries(surgescript.bin ${LIBSURGESCRIPT})
188 include_directories("${CMAKE_BINARY_DIR}/src")
189 target_compile_definitions(surgescript.bin PUBLIC ENABLE_THREADS=${ENABLE_THREADS})
190 target_link_libraries(surgescript.bin ${LIBSURGESCRIPT} ${LIBTHREAD})
168191 target_include_directories(surgescript.bin PRIVATE src)
169192 set_target_properties(surgescript.bin PROPERTIES OUTPUT_NAME surgescript)
193
194 # WebAssembly
195 if(EMSCRIPTEN)
196 target_link_options(surgescript.bin PRIVATE -Os --closure 1 -sMODULARIZE=1 -sMALLOC="emmalloc" -sEXPORTED_FUNCTIONS=["_main"]) # Reduce output size
197 endif()
170198
171199 # Windows icon
172200 if(WIN32 AND MINGW)
173201 if(NOT CMAKE_RC_COMPILER)
174202 set(CMAKE_RC_COMPILER windres)
175203 endif()
176 execute_process(COMMAND "${CMAKE_RC_COMPILER}" -O coff -o "${CMAKE_CURRENT_BINARY_DIR}/iconwin.res" -i "${CMAKE_SOURCE_DIR}/src/surgescript/misc/iconwin.rc" -I "${CMAKE_SOURCE_DIR}")
177 set_target_properties(surgescript.bin PROPERTIES LINK_FLAGS "-static-libgcc \"${CMAKE_CURRENT_BINARY_DIR}/iconwin.res\"")
204 execute_process(COMMAND "${CMAKE_RC_COMPILER}" -O coff -o "${CMAKE_BINARY_DIR}/iconwin.res" -i "${CMAKE_SOURCE_DIR}/src/surgescript/misc/iconwin.rc" -I "${CMAKE_SOURCE_DIR}")
205 set_target_properties(surgescript.bin PROPERTIES LINK_FLAGS "-static-libgcc \"${CMAKE_BINARY_DIR}/iconwin.res\"")
178206 endif()
179207
180208 # *nix metadata & icon
181209 if(UNIX)
182 generate_file("surgescript.appdata.xml")
183 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/surgescript.appdata.xml DESTINATION "${METAINFO_PATH}")
184 install(FILES src/surgescript/misc/surgescript.png DESTINATION "${ICON_PATH}")
210 file(TO_CMAKE_PATH "${CMAKE_INSTALL_DATADIR}/${ICON_PATH}/surgescript.png" ICON_FILEPATH)
211 generate_file("misc/surgescript.appdata.xml")
212 install(FILES "${CMAKE_BINARY_DIR}/src/surgescript/misc/surgescript.appdata.xml" DESTINATION "${CMAKE_INSTALL_DATADIR}/${METAINFO_PATH}")
213 install(FILES src/surgescript/misc/surgescript.png DESTINATION "${CMAKE_INSTALL_DATADIR}/${ICON_PATH}")
185214 endif()
186215
187216 # Installing the executable
188 install(TARGETS surgescript.bin DESTINATION bin)
189 endif()
217 install(TARGETS surgescript.bin DESTINATION "${CMAKE_INSTALL_BINDIR}")
218 endif()
5151
5252 Reference to the Animation object of the Actor.
5353
54 #### alpha
55
56 `alpha`: number.
57
58 Opacity value, ranging from zero (0% opaque) to one (100% opaque). Defaults to 1.0.
59
6054 #### entity
6155
6256 `entity`: object, read-only.
6357
6458 The entity associated with this component.
65
66 #### offset
67
68 `offset`: [Vector2](/engine/vector2) object.
69
70 A *(x,y)* offset relative to the parent object. Defaults to zero.
7159
7260 #### hflip
7361
8775
8876 Should the actor be rendered? Defaults to `true`.
8977
78 #### alpha
79
80 `alpha`: number.
81
82 Opacity value, ranging from zero (0% opaque) to one (100% opaque). Defaults to 1.0.
83
84 #### offset
85
86 `offset`: [Vector2](/engine/vector2) object.
87
88 A *(x,y)* offset relative to the parent object. Defaults to zero.
89
90 #### anchor
91
92 `anchor`: [Vector2](/engine/vector2) object.
93
94 A shortcut to `animation.anchor`. See also: [anchor](/engine/animation#anchor).
95
96 *Available since:* Open Surge 0.6.0
97
98 #### hotSpot
99
100 `hotSpot`: [Vector2](/engine/vector2) object.
101
102 A shortcut to `animation.hotSpot`. See also: [hot spot](/engine/animation#hotspot).
103
104 *Available since:* Open Surge 0.6.0. In versions prior to 0.6.0, you may get the hot spot using the [Animation](/engine/animation#hotspot) object.
105
106 #### actionSpot
107
108 `actionSpot`: [Vector2](/engine/vector2) object.
109
110 A shortcut to `animation.actionSpot`. See also: [action spot](/engine/animation#actionspot).
111
112 *Available since:* Open Surge 0.6.0
113
114 #### actionOffset
115
116 `actionOffset`: [Vector2](/engine/vector2) object, read-only.
117
118 A shortcut to `animation.actionOffset`. See also: [action offset](/engine/animation#actionoffset).
119
120 *Available since:* Open Surge 0.6.0
121
90122 #### width
91123
92124 `width`: number, read-only.
2626
2727 `id`: number.
2828
29 The number of the current animation, defined in a .spr file.
29 The number of the animation, defined in a .spr file.
3030
3131 #### sprite
3232
3838
3939 `exists`: boolean, read-only.
4040
41 Will be `true` if the current animation exists, i.e., if its sprite and its animation number have been defined in a .spr file.
41 Will be `true` if the animation exists, i.e., if its sprite and its animation number have been defined in a .spr file.
4242
4343 *Available since:* Open Surge 0.5.1
4444
4646
4747 `finished`: boolean, read-only.
4848
49 Will be `true` if the current animation has finished playing.
49 Will be `true` if the animation has finished playing.
5050
51 #### hotspot
51 #### anchor
5252
53 `hotspot`: [Vector2](/engine/vector2) object, read-only.
53 `anchor`: [Vector2](/engine/vector2) object, read-only.
5454
55 The position of the hot spot of the current animation.
55 The hot spot of the animation normalized to [0,1] x [0,1].
56
57 *Available since:* Open Surge 0.6.0
58
59 #### hotSpot
60
61 `hotSpot`: [Vector2](/engine/vector2) object, read-only.
62
63 The hot spot of the animation. Coordinates are given in pixels.
64
65 *Note:* prior to Open Surge 0.6.0, this property was called `hotspot`.
66
67 #### actionSpot
68
69 `actionSpot`: [Vector2](/engine/vector2) object, read-only.
70
71 The action spot of the animation. Coordinates are given in pixels. If the sprite is flipped, the action spot is automatically flipped relative to the [hot spot](#hotspot) of the animation.
72
73 *Available since:* Open Surge 0.6.0
74
75 #### actionOffset
76
77 `actionOffset`: [Vector2](/engine/vector2) object, read-only.
78
79 When this vector is added to the position of the sprite, you'll get the position of the [action spot](#actionspot). This is suitable to be used with [transform.localPosition](/engine/transform#localposition).
80
81 *Available since:* Open Surge 0.6.0
5682
5783 #### fps
5884
5985 `fps`: number, read-only.
6086
61 Frames per second of the current animation.
87 Frames per second of the animation.
6288
6389 #### repeats
6490
6591 `repeats`: boolean, read-only.
6692
67 Does the current animation repeat itself?
93 Does the animation repeat itself?
6894
6995 #### frameCount
7096
7197 `frameCount`: number, read-only.
7298
73 The number of frames of the current animation.
99 The number of frames of the animation.
74100
75101 #### frame
76102
88114
89115 `sync`: boolean.
90116
91 Is the current animation is synchronized? A synchronized animation is a repeating animation that displays the same frame across multiple sprites. Defaults to `false`.
117 Is the animation is synchronized? A synchronized animation is a repeating animation that displays the same frame across multiple sprites. Defaults to `false`.
7373
7474 Is the collider visible? This is useful for debugging. The default value is `false`.
7575
76 #### anchor
77
78 `anchor`: [Vector2](/engine/vector2) object.
79
80 The anchor of the collider. See also: [setAnchor](#setanchor).
81
82 *Available since:* Open Surge 0.6.0
83
7684 Functions
7785 ---------
7886
108116
109117 `setAnchor(x, y)`
110118
111 Defines the anchor of the collider to be (`x`, `y`), where these values are (usually) numbers between 0.0 and 1.0. Imagine a bounding box of the collider. Point (0.5, 0.5) is the default, representing its center. Point (0.0, 0.0) is the top-left and (1,0, 1.0), the bottom-right. The anchor of the collider will be aligned to the hot spot of the sprite of the entity.
119 Defines the anchor of the collider to be (`x`, `y`), where these values are (usually) numbers between 0.0 and 1.0. Imagine a bounding box of the collider. Point (0.5, 0.5) is the default, representing its center. Point (0.0, 0.0) is the top-left and (1,0, 1.0), the bottom-right. The anchor of the collider will be aligned to the hot spot of the sprite of the entity. See also: [anchor](#anchor).
112120
113121 *Arguments*
114122
141141
142142 `gravity`: number, read-only.
143143
144 A default value for the level gravity, in pixels per second squared.
144 The acceleration of gravity, measured in pixels per second squared.
145145
146146 #### time
147147
148 `time`: number, read-only.
148 `time`: number.
149149
150150 Elapsed time in the level, given in seconds.
151
152 *Note:* this property is writable since Open Surge 0.6.0.
151153
152154 #### next
153155
5757
5858 Jump speed, in pixels per second. The higher the value, the more intense the jump.
5959
60 #### gravityMultiplier
61
62 `gravityMultiplier`: number.
63
64 A multiplier used to modify how the Platformer is affected by gravity. When set to 1.0, the Platformer will subject to the [default acceleration of gravity](/engine/level#gravity). When set to 2.0 (0.5), the Platformer will be subject to twice (half) the default gravity, and so on. Zero means no gravity. Defaults to 1.0.
65
66 *Available since:* Open Surge 0.6.0
67
6068 #### direction
6169
6270 `direction`: number, read-only.
204212
205213 #### forceJump
206214
207 `forceJump()`
215 `forceJump(speed)`
208216
209217 Makes the platformer jump, regardless if it's touching the ground or not.
218
219 *Arguments*
220
221 * `speed`: number. Jump speed in pixels per second.
210222
211223 *Returns*
212224
226238 *Returns*
227239
228240 Returns the platformer itself.
241
242 #### showSensors
243
244 `showSensors()`
245
246 Displays the internal sensors of the platformer, which are invisible by default. For debugging only.
247
248 *Available since:* Open Surge 0.5.2
249
250 *Returns*
251
252 Returns the platformer itself.
253
254 *Example*
255
256 ```
257 using SurgeEngine.Actor;
258 using SurgeEngine.Behaviors.Platformer;
259
260 object "Testing Stuff" is "entity"
261 {
262 actor = Actor("Testing Stuff");
263 platformer = Platformer().showSensors();
264 // ...
265 }
266 ```
267
268 #### hideSensors
269
270 `hideSensors()`
271
272 Hides the internal sensors of the platformer. This is the default.
273
274 *Available since:* Open Surge 0.5.2
275
276 *Returns*
277
278 Returns the platformer itself.
176176 }
177177 ```
178178
179 #### animation
180
181 `animation`: [Animation](/engine/animation) object, read-only.
182
183 Reference to the Animation object of the Player.
184
179185 #### anim
180186
181187 `anim`: number.
182188
183 A shortcut to `animation.id`: an integer corresponding to the animation number.
184
185 #### animation
186
187 `animation`: [Animation](/engine/animation) object, read-only.
188
189 Reference to the Animation object of the Player.
189 A shortcut to `animation.id`: an integer corresponding to the animation number. See also: [Animation](/engine/animation), [id](/engine/animation#id).
190
191 #### anchor
192
193 `anchor`: [Vector2](/engine/vector2) object.
194
195 A shortcut to `animation.anchor`. See also: [anchor](/engine/animation#anchor).
196
197 *Available since:* Open Surge 0.6.0
198
199 #### hotSpot
200
201 `hotSpot`: [Vector2](/engine/vector2) object.
202
203 A shortcut to `animation.hotSpot`. See also: [hot spot](/engine/animation#hotspot).
204
205 *Available since:* Open Surge 0.6.0. In versions prior to 0.6.0, you may get the hot spot using the [Animation](/engine/animation#hotspot) object.
206
207 #### actionSpot
208
209 `actionSpot`: [Vector2](/engine/vector2) object.
210
211 A shortcut to `animation.actionSpot`. See also: [action spot](/engine/animation#actionspot).
212
213 *Available since:* Open Surge 0.6.0
214
215 #### actionOffset
216
217 `actionOffset`: [Vector2](/engine/vector2) object, read-only.
218
219 A shortcut to `animation.actionOffset`. See also: [action offset](/engine/animation#actionoffset).
220
221 *Available since:* Open Surge 0.6.0
190222
191223 #### attacking
192224
447479 `angle`: number.
448480
449481 The angle of the player, in degrees. The same as `transform.angle`.
482
483 #### slope
484
485 `slope`: number, read-only.
486
487 The angle detected by the physics system, in degrees. Unlike [angle](#angle), slope is read-only and does not feature any smoothing for visual aesthetics. Its purpose is to be a helper for physics calculations.
488
489 *Available since:* Open Surge 0.5.2
450490
451491 #### width
452492
1414 {
1515 Prefs["counter"] += 1;
1616 Console.print("counter: " + Prefs["counter"]);
17
18 state = "idle";
19 }
20
21 state "idle"
22 {
1723 }
1824
1925 fun constructor()
6363
6464 #### size
6565
66 `size`: [Vector2](/engine/vector2) object.
66 `size`: [Vector2](/engine/vector2) object, read-only.
6767
6868 The size, in pixels, of the rendered text.
6969
00 Welcome to SurgeScript!
11 =======================
22
3 <img src="img/surge.png" alt="Surge" align="right" width="384">
3 <img src="img/surge.png" alt="Surge" align="right" width="256">
44
55 Unleash your creativity!
66 ------------------------
1212 How do I learn SurgeScript?
1313 ---------------------------
1414
15 Check out the [SurgeScript Crash Course](/tutorials/hello)! Also take a look on the [video tutorials](https://youtube.com/alemart88) and on the examples folder that comes with the software.
15 Check out the [SurgeScript Crash Course](/tutorials/hello)! Additionally, take a look at the [video tutorials](https://youtube.com/alemart88) and at the examples that come with the software.
1616
1717 SurgeScript in a nutshell
1818 -------------------------
3232
3333 Unlike other programming languages, SurgeScript has been designed with the specific needs of games in mind. Its features include:
3434
35 - The state-machine pattern: objects are state machines, making it easy to create in-game entities
35 - The state-machine pattern: objects are state machines, making it easy to create game entities
3636 - The composition approach: you may design complex objects and behaviors by means of composition
3737 - The hierarchy system: objects have a parent and may have children, in a tree-like structure
3838 - The game loop: it's defined implicitly
3939 - Automatic garbage collection, object tagging and more!
4040
41 SurgeScript is meant to be used in games and in interactive applications. It's easy to integrate it into existing code, it's easy to extend, it features a C-like syntax, and it's free and open-source software.
41 SurgeScript is meant to be used in games and in interactive applications. It's easy to integrate it into existing code, it's easy to extend it, it features a C-like syntax, and it's free and open-source software.
4242
4343 SurgeScript has been designed based on the experience of its developer dealing with game engines, applications related to computer graphics and so on. Some of the best practices have been incorporated into the language itself, making things really easy for developers and modders.
4444
1111
1212 Reference to the parent object.
1313
14 #### childCount
15
16 `childCount`: number, read-only.
14 #### __name
15
16 `__name`: string, read-only.
17
18 The name of the object.
19
20 #### __active
21
22 `__active`: boolean.
23
24 Indicates whether the object is active or not.
25
26 Objects are active by default. Whenever an object is set to be inactive, its state machine is paused. Additionally, the state machines of all its descendants are also paused.
27
28 #### __functions
29
30 `__functions`: [Array](/reference/array) object, read-only.
31
32 The functions of this object represented as a collection of strings.
33
34 #### __timespent
35
36 `__timespent`: number, read-only.
37
38 The approximate time spent in the current state (in seconds).
39
40 #### __file
41
42 `__file`: string, read-only.
43
44 The source file of this object.
45
46 *Available since:* SurgeScript 0.5.3
47
48 #### __children
49
50 `__children`: [Array](/reference/array) object, read-only.
51
52 The children of this object.
53
54 *Available since:* SurgeScript 0.5.4
55
56 *Returns*
57
58 A new array featuring all the children of this object. If there are no children, an empty array is returned.
59
60 #### __childCount
61
62 `__childCount`: number, read-only.
1763
1864 The number of children of the object.
1965
20 #### __name
21
22 `__name`: string, read-only.
23
24 The name of the object.
25
26 #### __active
27
28 `__active`: boolean.
29
30 Indicates whether the object is active or not.
31
32 Objects are active by default. Whenever an object is set to be inactive, its state machine is paused. Additionally, the state machines of all its descendants are also paused.
33
34 #### __functions
35
36 `__functions`: [Array](/reference/array) object, read-only.
37
38 The functions of this object represented as a collection of strings.
39
40 #### __timespent
41
42 `__timespent`: number, read-only.
43
44 The approximate time spent in this object in the last frame (in seconds).
45
46 #### __file
47
48 `__file`: string, read-only.
49
50 The source file of this object.
51
52 *Available since:* SurgeScript 0.5.3
66 *Available since:* SurgeScript 0.5.5
67
68 *Note:* prior to SurgeScript 0.5.5, you would use `object.childCount` instead. That form is now obsolete.
69
5370
5471 Functions
5572 ---------
77
88 Some programming languages, such as C++, have a feature called *operator overloading*. It's a *syntactic sugar* that allows the programmer to attribute custom implementations to different operators.
99
10 In SurgeScript, the `[]` operator (also called the *lookup operator*), used by [Arrays](/reference/array) and [Dictionaries](/reference/dictionary), is used to **get** and **set** values from/to the data structure. In fact, the `[]` operator can be used with any object. It is necessary to define, in your object, functions `get()` and `set()` with the following signature:
10 In SurgeScript, the `[]` operator (also called the *lookup operator*), used by [Arrays](/reference/array) and [Dictionaries](/reference/dictionary), is used to **get** and **set** values from/to the data structure. In fact, the `[]` operator can be used with any object. It is necessary to define, in your object, functions named `get()` and `set()` with the following signature:
1111
1212 ```
1313 fun get(key)
44 - Follow the golden rule: objects should **not** mess with each others' internals!
55 - Objects should **not** change others' internal variables or states directly (not allowed!)
66 - Objects should define [functions](/tutorials/functions) that can be operated upon by the external world (API)
7 - Use a consistent style, e.g.,
7 - Use a consistent coding style. Suggestion:
88 - Use *camelCase* names for both variables and functions.
99 - Use *4 spaces* when indenting your code.
1010 - Combine related [packages](/tutorials/packages) into a single one: don't pollute the global namespace.
108108
109109 Expression|Result
110110 ----------|------
111 `a && b` |`true` only if both a and b are true
112 `a || b` | `true` if a is true or b is true
113 `!a` | `true` if a is false, `false` if a is true
111 `a && b` |`true` only if both `a` and `b` are true
112 `a || b` | `true` if `a` is true or `b` is true
113 `!a` | `true` if `a` is false, `false` if `a` is true
114114
115115 Notice that **not** has higher precedence than the other two operators. Examples:
116116
8282 Function / property|Description
8383 -------------------|-----------
8484 `obj.parent` (read-only) | The parent object
85 `obj.childCount` (read-only) | Number of immediate children
8685 `obj.child(name)` | Gets a child object named `name`
8786 `obj.findObject(name)` | Finds a descendant named `name`
87 `obj.__childCount` (read-only) | Number of immediate children
8888
8989 Example:
9090
9696
9797 state "main"
9898 {
99 Console.print("This object has " + this.childCount + " children.");
99 Console.print("This object has " + this.__childCount + " children.");
100100 destroy();
101101 }
102102 }
11 site_url: 'https://docs.opensurge2d.org/'
22 site_author: 'Alexandre Martins'
33 site_description: 'SurgeScript is a scripting language for games.'
4 copyright: 'SurgeScript is a scripting language for games. Copyright © 2016-2020 Alexandre Martins.'
4 site_dir: 'docs_html/'
5 copyright: 'SurgeScript is a scripting language for games. Copyright © 2016-2021 Alexandre Martins.'
56 google_analytics: ['UA-120511928-1', 'opensurge2d.org']
67 repo_url: 'https://github.com/alemart/surgescript'
7 strict: true
8 strict: false
89 theme:
910 name: 'material'
1011 nav:
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2020 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2022 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
1515 * limitations under the License.
1616 *
1717 * runtime/main.c
18 * SurgeScript Runtime Engine entry point
18 * SurgeScript CLI
1919 */
2020
2121 #include <surgescript.h>
2222 #include <string.h>
2323 #include <stdio.h>
2424
25 static surgescript_vm_t* make_vm(int argc, char** argv);
25 /* multithread support */
26 #if ENABLE_THREADS
27 # if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
28 # include <threads.h>
29 # else
30 # error "Can't compile the SurgeScript CLI: threads.h is not found on this environment. Please change the environment or disable multithreading."
31 # endif
32 #endif
33
34 static surgescript_vm_t* make_vm(int argc, char** argv, uint64_t* time_limit);
35 static void run_vm(surgescript_vm_t* vm, uint64_t time_limit);
36 static void destroy_vm(surgescript_vm_t* vm);
2637 static void print_to_stdout(const char* message);
2738 static void print_to_stderr(const char* message);
2839 static void discard_message(const char* message);
2940 static void show_help(const char* executable);
41 static char* read_from_stdin();
42 static int main_loop(void* arg);
43
44 /* default time limit, given in milliseconds */
45 #define DEFAULT_TIME_LIMIT 30000
3046
3147 /*
3248 * main()
3450 */
3551 int main(int argc, char* argv[])
3652 {
37 if(argc > 1) {
38 /* create the VM and compile the input file(s) */
39 surgescript_vm_t* vm = make_vm(argc, argv);
40 if(vm != NULL) {
41 /* run the VM */
42 while(surgescript_vm_update(vm)) {
43 ;
44 }
45
46 /* destroy the VM */
47 surgescript_vm_destroy(vm);
48 }
49 }
50 else {
51 /* print usage */
52 show_help(surgescript_util_basename(argv[0]));
53 uint64_t time_limit = DEFAULT_TIME_LIMIT;
54
55 /* Create the VM and compile the input file(s) */
56 surgescript_vm_t* vm = make_vm(argc, argv, &time_limit);
57
58 /* got a VM? */
59 if(vm != NULL) {
60 /* run the VM */
61 run_vm(vm, time_limit);
62
63 /* destroy the VM */
64 destroy_vm(vm);
5365 }
5466
5567 /* done! */
5668 return 0;
69 }
70
71 /**
72 * run_vm()
73 * Run the VM with a time limit
74 */
75 void run_vm(surgescript_vm_t* vm, uint64_t time_limit)
76 {
77 uint64_t start_time = surgescript_util_gettickcount();
78 #define show_time_limit_error() \
79 fprintf(stderr, "Time limit of %.1lf seconds exceeded.\n", (double)time_limit * 0.001)
80
81 #if !ENABLE_THREADS
82
83 /* main loop */
84 while(surgescript_vm_update(vm)) {
85
86 /* time limit */
87 if(time_limit > 0 && surgescript_util_gettickcount() > start_time + time_limit) {
88 show_time_limit_error();
89 break;
90 }
91
92 }
93
94 #else
95
96 /* run the SurgeScript VM on a separate thread */
97 thrd_t thread;
98 thrd_create(&thread, main_loop, vm);
99
100 /* handle the time limit, if it's been set */
101 if(time_limit > 0) {
102 while(surgescript_vm_is_active(vm)) {
103 if(surgescript_util_gettickcount() > start_time + time_limit) {
104 show_time_limit_error();
105 exit(1); /* TODO we should kill the other thread instead */
106 }
107
108 thrd_yield();
109 }
110 }
111
112 /* wait for the other thread to complete */
113 thrd_join(thread, NULL);
114
115 #endif
116
117 }
118
119 /**
120 * destroy_vm()
121 * Destroy a SurgeScript VM
122 */
123 void destroy_vm(surgescript_vm_t* vm)
124 {
125 surgescript_vm_destroy(vm);
126 }
127
128 /**
129 * main_loop()
130 * Game loop for multithreaded execution
131 */
132 int main_loop(void* arg)
133 {
134 #if !ENABLE_THREADS
135
136 (void)arg;
137 return 0;
138
139 #else
140
141 surgescript_vm_t* vm = (surgescript_vm_t*)arg;
142
143 while(surgescript_vm_update(vm)) {
144 thrd_yield();
145 }
146
147 return 0;
148
149 #endif
57150 }
58151
59152 /*
61154 * Parses the command line arguments and creates a VM
62155 * with the compiled scripts
63156 */
64 surgescript_vm_t* make_vm(int argc, char** argv)
157 surgescript_vm_t* make_vm(int argc, char** argv, uint64_t* time_limit)
65158 {
66159 surgescript_vm_t* vm = NULL;
67160 int i;
86179 show_help(surgescript_util_basename(argv[0]));
87180 return NULL;
88181 }
182 else if(strcmp(arg, "--timelimit") == 0 || strcmp(arg, "-t") == 0) {
183 /* set time limit (maximum execution time) */
184 if(++i < argc && time_limit != NULL) {
185 double seconds = atof(argv[i]);
186 *time_limit = (seconds > 0.0) ? (uint64_t)(seconds * 1000.0) : 0;
187 }
188 }
189 else if(strcmp(arg, "--") == 0) {
190 /* user-specific command line arguments */
191 break;
192 }
89193 else {
90 printf("Unrecognized option: '%s'.\nType '%s --help' for more information.\n", arg, surgescript_util_basename(argv[0]));
194 /* unrecognized option */
195 fprintf(stderr, "Unrecognized option: '%s'.\nType '%s --help' for more information.\n", arg, surgescript_util_basename(argv[0]));
91196 return NULL;
92197 }
93198 }
96201 vm = surgescript_vm_create();
97202
98203 /* compile the scripts */
99 for(; i < argc && strcmp(argv[i], "--") != 0; i++) {
100 const char* file = argv[i];
101 surgescript_vm_compile(vm, file);
204 if(i < argc && strcmp(argv[i], "--") != 0) {
205 /* read files */
206 for(; i < argc && strcmp(argv[i], "--") != 0; i++) {
207 const char* file = argv[i];
208 surgescript_vm_compile(vm, file);
209 }
210 }
211 else {
212 fprintf(stderr, "Reading from stdin... Run '%s -h' for help.\n", surgescript_util_basename(argv[0]));
213
214 /* read from stdin */
215 char* code = read_from_stdin();
216 surgescript_vm_compile_code_in_memory(vm, code);
217 ssfree(code);
102218 }
103219
104220 /* launch the VM */
105 surgescript_vm_launch_ex(vm, argc, argv);
221 if(i < argc && strcmp(argv[i], "--") == 0) {
222 /* launch with user-specific command line arguments */
223 ++i;
224 surgescript_vm_launch_ex(vm, argc - i, (char**)(argv + i));
225 }
226 else {
227 /* launch without user-specific command line arguments */
228 surgescript_vm_launch(vm);
229 }
106230
107231 /* done! */
108232 return vm;
125249 "Options:\n"
126250 " -v, --version shows the version of SurgeScript\n"
127251 " -D, --debug prints debugging information\n"
252 " -t, --timelimit sets a maximum execution time, in seconds (0 = no limit)\n"
128253 " -h, --help shows this message\n"
129254 "\n"
130255 "Examples:\n"
132257 " %s file1.ss file2.ss compiles and executes file1.ss and file2.ss\n"
133258 " %s --debug test.ss compiles and runs test.ss with debugging information\n"
134259 " %s file.ss -- -x -y passes custom arguments -x and -y to file.ss\n"
260 " %s -t 5 runs a script read from stdin, with a time limit of 5 seconds\n"
135261 "\n"
136 "Full documentation at: <%s>\n",
262 "Full documentation available at: <%s>\n",
137263 surgescript_util_version(),
138264 surgescript_util_year(),
139265 surgescript_util_authors(),
143269 executable,
144270 executable,
145271 executable,
272 executable,
146273 surgescript_util_website()
147274 );
148275 }
172299 void discard_message(const char* message)
173300 {
174301 ;
175 }
302 }
303
304 /**
305 * read_from_stdin()
306 * Read data from stdin and store it in a string
307 */
308 char* read_from_stdin()
309 {
310 const size_t BUFSIZE = 1024;
311 char* data = NULL;
312 size_t read_chars = 0, data_size = 0;
313
314 /* read to data[] */
315 do {
316 data_size += BUFSIZE;
317 data = ssrealloc(data, data_size + 1);
318 read_chars += fread(data + read_chars, sizeof(char), BUFSIZE, stdin);
319 data[read_chars] = '\0';
320 } while(read_chars == data_size);
321
322 /* done! */
323 return data;
324 }
190190 {
191191 FILE* fp = surgescript_util_fopen_utf8(absolute_path, "rb"); /* use binary mode, so offsets don't get messed up */
192192 if(fp) {
193 static size_t BUFSIZE = 1024;
193 const size_t BUFSIZE = 1024;
194194 char* data = NULL;
195195 size_t read_chars = 0, data_size = 0;
196196
11041104 if(!is_state_context(context))
11051105 ssfatal("Compile Error: timeout can only be used inside a state (see %s:%d).", context.source_file, surgescript_token_linenumber(parser->previous));
11061106 match(parser, SSTOK_LPAREN);
1107 expr(parser, context);
1107 assignexpr(parser, context);
11081108 emit_timeout(context);
11091109 match(parser, SSTOK_RPAREN);
11101110 }
7676 * | ! <unaryexpr>
7777 * | typeof <unaryexpr> | typeof ( <expr> )
7878 * | ++ identifier | -- identifier
79 * | timeout ( <expr> )
79 * | timeout ( <assignexpr> )
8080 * | <postfixexpr>
8181 * <postfixexpr> := identifier ++ | identifier --
8282 * | <funcallexpr> <postfixexpr1>
154154 void surgescript_parser_set_flags(surgescript_parser_t* parser, surgescript_parser_flags_t flags); /* set parser options (flags) */
155155 surgescript_parser_flags_t surgescript_parser_get_flags(surgescript_parser_t* parser); /* get parser flags */
156156
157 #endif
157 #endif
0 /* Project info */
0 /* SurgeScript info */
1 #include "../util/version.h"
2
3 #cmakedefine PROJECT_YEARS "@PROJECT_YEARS@"
4 #if !defined(PROJECT_YEARS)
5 #error Undefined PROJECT_YEARS
6 #endif
7
18 const char SURGESCRIPT_AUTHORS[] = "Alexandre Martins";
2 const char SURGESCRIPT_YEARS[] = "2016-2020";
39 const char SURGESCRIPT_WEBSITE[] = "https://github.com/alemart/surgescript";
4
5 /* compiled version of SurgeScript */
6 #cmakedefine PROJECT_VERSION "@PROJECT_VERSION@"
7
8 #if defined(PROJECT_VERSION)
9 const char SURGESCRIPT_VERSION[] = PROJECT_VERSION;
10 #else
11 #error "Undefined PROJECT_VERSION (major.minor.patch.tweak)"
12 #endif
10 const char SURGESCRIPT_VERSION[] = SURGESCRIPT_VERSION_STR;
11 const char SURGESCRIPT_YEARS[] = PROJECT_YEARS;
0 #!/bin/bash
1 # This utility packs the Windows build of SurgeScript
2 # Copyright 2016-2021 Alexandre Martins
3
4 UNIX2DOS="todos" #"unix2dos"
5 SOURCE_FOLDER="../../.."
6 BUILD_FOLDER="$SOURCE_FOLDER/build"
7 OUTPUT_FOLDER="/tmp"
8
9 # File surgescript.exe must be present before packaging
10 if [ ! -f "$BUILD_FOLDER/surgescript.exe" ]; then
11 echo "File surgescript.exe not found"
12 exit 1
13 fi
14
15 # Extract the SurgeScript version
16 VERSION=$(wine "$BUILD_FOLDER/surgescript.exe" -v | tr -cd [:digit:][:punct:])
17 PACKAGE="surgescript-$VERSION-win"
18 echo "Found SurgeScript version $VERSION."
19
20 # Create a temporary folder for packaging
21 TMP_FOLDER="/tmp/$PACKAGE"
22 rm -rf "$TMP_FOLDER" 2>/dev/null
23 mkdir -p "$TMP_FOLDER"
24
25 # Write a README for Windows
26 cat > "$TMP_FOLDER/README-Windows.txt" << EOF
27 --------------------------------------------------
28 SurgeScript
29 A scripting language for games
30 Copyright (C) 2016-$(date +%Y) Alexandre Martins
31 --------------------------------------------------
32 This is the Windows build of SurgeScript $VERSION.
33
34 To test SurgeScript, run the surgescript executable via the Command Prompt.
35 Pass the scripts you want to test via the command line, as in the examples:
36
37 ** See the available options: **
38 C:\path\to\surgescript> surgescript
39
40 ** Run a test script: **
41 C:\path\to\surgescript> surgescript examples\hello.ss
42
43 ** Run another test script: **
44 C:\path\to\surgescript> surgescript examples\count_to_10.ss
45
46 There are many example scripts to try out. Check the examples folder for
47 more information.
48
49 Visit the SurgeScript website at: https://github.com/alemart/surgescript/
50 EOF
51
52 # Copy files
53 for file in surgescript.exe libsurgescript-static.a libsurgescript.dll.a libsurgescript.dll surgescript.pc surgescript-static.pc; do
54 echo "Copying $file..."
55 cp "$BUILD_FOLDER/$file" "$TMP_FOLDER"
56 done
57
58 for file in LICENSE README.md CHANGES.md CMakeLists.txt mkdocs.yml; do
59 echo "Copying $file..."
60 cp "$SOURCE_FOLDER/$file" "$TMP_FOLDER"
61 done
62
63 for folder in examples src cmake; do
64 echo "Copying $folder/ ..."
65 cp -r "$SOURCE_FOLDER/$folder" "$TMP_FOLDER"
66 done
67
68 mv "$TMP_FOLDER/LICENSE" "$TMP_FOLDER/LICENSE.txt"
69
70 # Converting newlines of all text files
71 for f in `find "$TMP_FOLDER" -type f -exec grep -Iq . {} \; -print`; do
72 ${UNIX2DOS} $f
73 done
74
75 # Generate the docs
76 pushd "$SOURCE_FOLDER"
77 mkdocs build
78 popd
79
80 # Copying the docs
81 for folder in docs docs_html; do
82 echo "Copying $folder/ ..."
83 cp -r "$SOURCE_FOLDER/$folder" "$TMP_FOLDER"
84 done
85
86 # Create the .zip package
87 pushd "$TMP_FOLDER"
88 echo "Packaging..."
89 zip -r "$OUTPUT_FOLDER/$PACKAGE.zip" ./*
90 echo "Packaged to $OUTPUT_FOLDER/$PACKAGE.zip"
91 popd
92 #rm -rf "${TMP_FOLDER}"
00 <?xml version="1.0" encoding="UTF-8"?>
1 <!-- Copyright 2020 Alexandre Martins -->
21 <component type="console-application">
32 <id>org.opensurge2d.SurgeScript</id>
43 <name>SurgeScript</name>
54 <developer_name>Alexandre Martins</developer_name>
65 <update_contact>alemartf_at_gmail.com</update_contact>
7 <metadata_license>MIT</metadata_license>
6 <metadata_license>Apache-2.0</metadata_license>
87 <project_license>Apache-2.0</project_license>
98 <summary>A scripting language for games</summary>
109 <description>
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2019 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2019, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
2727 #include "heap.h"
2828 #include "stack.h"
2929 #include "renv.h"
30 #include "vm_time.h"
3031 #include "../util/transform.h"
3132 #include "../util/ssarray.h"
3233 #include "../util/util.h"
5152 bool is_active; /* can i run programs? */
5253 bool is_killed; /* am i scheduled to be destroyed? */
5354 bool is_reachable; /* is this object reachable through some other? (garbage-collection) */
55
56 /* internal timer */
57 const surgescript_vmtime_t* vmtime; /* VM time */
5458 uint64_t last_state_change; /* moment of the last state change */
5559 uint64_t time_spent; /* how much time did this object consume since the last state change */
5660
8892 * surgescript_object_create()
8993 * Creates a new blank object
9094 */
91 surgescript_object_t* surgescript_object_create(const char* name, unsigned handle, surgescript_objectmanager_t* object_manager, surgescript_programpool_t* program_pool, surgescript_stack_t* stack, void* user_data)
95 surgescript_object_t* surgescript_object_create(const char* name, unsigned handle, surgescript_objectmanager_t* object_manager, surgescript_programpool_t* program_pool, surgescript_stack_t* stack, const surgescript_vmtime_t* vmtime, void* user_data)
9296 {
9397 surgescript_object_t* obj = ssmalloc(sizeof *obj);
9498
106110
107111 obj->state_name = ssstrdup(MAIN_STATE);
108112 obj->current_state = get_state_program(obj, obj->state_name);
109 obj->last_state_change = surgescript_util_gettickcount();
110 obj->time_spent = 0;
111113 obj->is_active = true;
112114 obj->is_killed = false;
113115 obj->is_reachable = false;
116
117 obj->vmtime = vmtime;
118 obj->last_state_change = surgescript_vmtime_time(obj->vmtime);
119 obj->time_spent = 0;
114120
115121 obj->transform = NULL;
116122 obj->user_data = user_data;
573579 ssfree(object->state_name);
574580 object->state_name = ssstrdup(state_name ? state_name : MAIN_STATE);
575581 object->current_state = get_state_program(object, object->state_name);
576 object->last_state_change = surgescript_util_gettickcount();
582 object->last_state_change = surgescript_vmtime_time(object->vmtime);
577583 object->time_spent = 0;
578584 }
579585 }
584590 */
585591 double surgescript_object_elapsed_time(const surgescript_object_t* object)
586592 {
587 return (surgescript_util_gettickcount() - object->last_state_change) * 0.001;
593 return (surgescript_vmtime_time(object->vmtime) - object->last_state_change) * 0.001;
588594 }
589595
590596 /*
835841 {
836842 uint64_t now = surgescript_util_gettickcount();
837843 uint64_t dt = now > object->last_state_change ? now - object->last_state_change : 1;
838 return (double)(object->time_spent * 0.001) / dt;
844 return ((double)(object->time_spent) * 0.001) / ((double)dt);
839845 }
840846
841847 /*
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2018 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2018, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
2323 #include "object.h"
2424 #include "program_pool.h"
2525 #include "tag_system.h"
26 #include "vm_time.h"
2627 #include "stack.h"
2728 #include "heap.h"
2829 #include "variable.h"
4243 surgescript_stack_t* stack; /* reference to the stack */
4344 surgescript_tagsystem_t* tag_system; /* tag system */
4445 surgescript_vmargs_t* args; /* VM command-line arguments (NULL-terminated array) */
46 const surgescript_vmtime_t* vmtime; /* VM time */
4547 SSARRAY(surgescript_objecthandle_t, objects_to_be_scanned); /* garbage collection */
4648 int first_object_to_be_scanned; /* an index of objects_to_be_scanned */
4749 int reachables_count; /* garbage-collector stuff */
7981 }; /* this must be a NULL-terminated array */
8082
8183 /* object methods acessible by me */
82 extern surgescript_object_t* surgescript_object_create(const char* name, unsigned handle, struct surgescript_objectmanager_t* object_manager, struct surgescript_programpool_t* program_pool, struct surgescript_stack_t* stack, void* user_data); /* creates a new blank object */
84 extern surgescript_object_t* surgescript_object_create(const char* name, unsigned handle, surgescript_objectmanager_t* object_manager, surgescript_programpool_t* program_pool, surgescript_stack_t* stack, const surgescript_vmtime_t* vmtime, void* user_data); /* creates a new blank object */
8385 extern surgescript_object_t* surgescript_object_destroy(surgescript_object_t* object); /* destroys an object */
8486
8587 /* the life-cycle of the objects is handled by me */
110112 * surgescript_objectmanager_create()
111113 * Creates a new object manager
112114 */
113 surgescript_objectmanager_t* surgescript_objectmanager_create(surgescript_programpool_t* program_pool, surgescript_tagsystem_t* tag_system, surgescript_stack_t* stack, surgescript_vmargs_t* args)
115 surgescript_objectmanager_t* surgescript_objectmanager_create(surgescript_programpool_t* program_pool, surgescript_tagsystem_t* tag_system, surgescript_stack_t* stack, surgescript_vmargs_t* args, const surgescript_vmtime_t* vmtime)
114116 {
115117 surgescript_objectmanager_t* manager = ssmalloc(sizeof *manager);
116118
122124 manager->tag_system = tag_system;
123125 manager->stack = stack;
124126 manager->args = args;
127 manager->vmtime = vmtime;
125128 manager->handle_ptr = ROOT_HANDLE;
126129
127130 ssarray_init(manager->objects_to_be_scanned);
161164 {
162165 surgescript_objecthandle_t handle = new_handle(manager);
163166 surgescript_object_t *parent_object = surgescript_objectmanager_get(manager, parent);
164 surgescript_object_t *object = surgescript_object_create(object_name, handle, manager, manager->program_pool, manager->stack, user_data);
167 surgescript_object_t *object = surgescript_object_create(object_name, handle, manager, manager->program_pool, manager->stack, manager->vmtime, user_data);
165168
166169 /* store the object */
167170 if(handle >= ssarray_length(manager->data) && handle > ROOT_HANDLE) {
203206 char** data[] = { (char**)SYSTEM_OBJECTS, plugins };
204207
205208 /* spawn the root object */
206 surgescript_object_t *object = surgescript_object_create(ROOT_OBJECT, ROOT_HANDLE, manager, manager->program_pool, manager->stack, data);
209 surgescript_object_t *object = surgescript_object_create(ROOT_OBJECT, ROOT_HANDLE, manager, manager->program_pool, manager->stack, manager->vmtime, data);
207210 ssarray_push(manager->data, object);
208211 manager->count++;
209212
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2018 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2018, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
3333 struct surgescript_stack_t;
3434 struct surgescript_tagsystem_t;
3535 struct surgescript_vmargs_t;
36 struct surgescript_vmtime_t;
3637
3738
3839 /* public methods */
3940
4041 /* life-cycle */
41 surgescript_objectmanager_t* surgescript_objectmanager_create(struct surgescript_programpool_t* program_pool, struct surgescript_tagsystem_t* tag_system, struct surgescript_stack_t* stack, struct surgescript_vmargs_t* args);
42 surgescript_objectmanager_t* surgescript_objectmanager_create(struct surgescript_programpool_t* program_pool, struct surgescript_tagsystem_t* tag_system, struct surgescript_stack_t* stack, struct surgescript_vmargs_t* args, const struct surgescript_vmtime_t* vmtime);
4243 surgescript_objectmanager_t* surgescript_objectmanager_destroy(surgescript_objectmanager_t* manager);
4344
4445 /* operations */
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2019 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
517517 break;
518518
519519 case SSOP_DIV:
520 if(fast_notzero(surgescript_var_get_number(t(b))))
521 surgescript_var_set_number(t(a), surgescript_var_get_number(t(a)) / surgescript_var_get_number(t(b)));
522 else if(fast_sign(surgescript_var_get_number(t(a))) >= 0)
523 surgescript_var_set_number(t(a), INFINITY * fast_sign1(surgescript_var_get_number(t(b))));
524 else
525 surgescript_var_set_number(t(a), -INFINITY * fast_sign1(surgescript_var_get_number(t(b))));
520 /* division by zero should follow the IEEE-754 */
521 surgescript_var_set_number(t(a), surgescript_var_get_number(t(a)) / surgescript_var_get_number(t(b)));
526522 break;
527523
528524 case SSOP_MOD:
589585 *ip = a.u;
590586 return;
591587 }
592 else
593 break;
588 break;
594589
595590 case SSOP_JNE:
596591 if(surgescript_var_get_rawbits(_t[2])) {
597592 *ip = a.u;
598593 return;
599594 }
600 else
601 break;
595 break;
602596
603597 case SSOP_JL:
604598 if(surgescript_var_get_rawbits(_t[2]) < 0) {
605599 *ip = a.u;
606600 return;
607601 }
608 else
609 break;
602 break;
610603
611604 case SSOP_JG:
612605 if(surgescript_var_get_rawbits(_t[2]) > 0) {
613606 *ip = a.u;
614607 return;
615608 }
616 else
617 break;
609 break;
618610
619611 case SSOP_JLE:
620612 if(surgescript_var_get_rawbits(_t[2]) <= 0) {
621613 *ip = a.u;
622614 return;
623615 }
624 else
625 break;
616 break;
626617
627618 case SSOP_JGE:
628619 if(surgescript_var_get_rawbits(_t[2]) >= 0) {
629620 *ip = a.u;
630621 return;
631622 }
632 else
633 break;
623 break;
634624
635625 /* function calls */
636626 case SSOP_CALL:
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2019 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2019, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
7878 surgescript_vm_bind(vm, "Object", "children", fun_children, 1);
7979 surgescript_vm_bind(vm, "Object", "childWithTag", fun_childwithtag, 1);
8080 surgescript_vm_bind(vm, "Object", "childrenWithTag", fun_childrenwithtag, 1);
81 surgescript_vm_bind(vm, "Object", "get_childCount", fun_childcount, 0);
8281 surgescript_vm_bind(vm, "Object", "findObject", fun_findobject, 1);
8382 surgescript_vm_bind(vm, "Object", "findObjects", fun_findobjects, 1);
8483 surgescript_vm_bind(vm, "Object", "findObjectWithTag", fun_findobjectwithtag, 1);
10099 surgescript_vm_bind(vm, "Object", "get___timespent", fun_timespent, 0);
101100 surgescript_vm_bind(vm, "Object", "get___memspent", fun_memspent, 0);
102101 surgescript_vm_bind(vm, "Object", "get___file", fun_file, 0);
102 surgescript_vm_bind(vm, "Object", "get___childCount", fun_childcount, 0);
103 surgescript_vm_bind(vm, "Object", "get_childCount", fun_childcount, 0); /* obsolete since 0.5.5 */
103104 }
104105
105106
6767 ssassert(START_ADDR == surgescript_heap_malloc(heap));
6868
6969 surgescript_var_set_number(surgescript_heap_at(heap, TIME_ADDR), 0.0);
70 surgescript_var_set_number(surgescript_heap_at(heap, DELTA_ADDR), 0.016);
70 surgescript_var_set_number(surgescript_heap_at(heap, DELTA_ADDR), 0.01667);
7171 surgescript_var_set_number(surgescript_heap_at(heap, START_ADDR), surgescript_util_gettickcount() * 0.001);
7272
7373 return NULL;
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2019 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2019, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
298298 switch(var->type) {
299299 case SSVAR_NULL:
300300 return ssstrdup("null");
301
301302 case SSVAR_BOOL:
302303 return ssstrdup(var->boolean ? "true" : "false");
304
303305 case SSVAR_STRING:
304306 return ssstrdup(var->string);
307
305308 case SSVAR_NUMBER: {
306309 char buf[32];
307310 surgescript_var_to_string(var, buf, sizeof(buf));
308311 return ssstrdup(buf);
309312 }
313
310314 case SSVAR_OBJECTHANDLE: {
311315 if(manager != NULL) {
312316 surgescript_object_t* obj = surgescript_objectmanager_get(manager, var->handle);
319323 else
320324 return ssstrdup("[object]");
321325 }
326
322327 case SSVAR_RAW:
323328 return ssstrdup("<raw>");
329
330 default:
331 return ssstrdup("<unknown>");
324332 }
325333 }
326334
334342 switch(var->type) {
335343 case SSVAR_OBJECTHANDLE:
336344 return var->handle;
345
337346 case SSVAR_NUMBER:
338347 return surgescript_objectmanager_system_object(NULL, "Number");
348
339349 case SSVAR_STRING:
340350 return surgescript_objectmanager_system_object(NULL, "String");
351
341352 case SSVAR_BOOL:
342353 return surgescript_objectmanager_system_object(NULL, "Boolean");
343 case SSVAR_NULL:
344 return surgescript_objectmanager_null(NULL);
345 case SSVAR_RAW:
354
355 default:
346356 return surgescript_objectmanager_null(NULL);
347357 }
348358 }
520530 case SSVAR_NUMBER: {
521531 /* encourage users to use approximatelyEqual() */
522532 /* epsilon comparisons may cause underlying problems, e.g., with infinity */
523 return (a->number > b->number) - (a->number < b->number);
533 return isgreater(a->number, b->number) - isless(a->number, b->number);
524534 }
525535 case SSVAR_RAW:
526536 return (a->raw > b->raw) - (a->raw < b->raw);
559569 unsigned long y = surgescript_var_get_objecthandle(b);
560570 return (x > y) - (x < y);
561571 }
562 else
563 return 0; /* this shouldn't happen */
564 }
572 }
573
574 /* this shouldn't happen */
575 return 0;
565576 }
566577
567578 /*
747758 /* Deallocates a bucket (must be fast) */
748759 void free_bucket(surgescript_varbucket_t* bucket)
749760 {
761 /* can't free if not in use */
762 ssassert(bucket->in_use);
763
764 /* put the bucket back in the pool */
750765 bucket->in_use = false;
751766 bucket->next = varpool_currbucket;
752767 varpool_currbucket = bucket;
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2018 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2018, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
2626 #include "program_pool.h"
2727 #include "tag_system.h"
2828 #include "object_manager.h"
29 #include "vm_time.h"
2930 #include "sslib/sslib.h"
3031 #include "../compiler/parser.h"
3132 #include "../util/util.h"
6162 surgescript_objectmanager_t* object_manager;
6263 surgescript_parser_t* parser;
6364 surgescript_vmargs_t* args;
64 double start_time;
65 surgescript_vmtime_t* time;
66 bool is_paused;
6567 };
6668
6769 /* misc */
68 static void create_vm_components(surgescript_vm_t* vm);
69 static void destroy_vm_components(surgescript_vm_t* vm);
70 static void setup_sslib(surgescript_vm_t* vm);
70 static void init_vm(surgescript_vm_t* vm);
71 static void release_vm(surgescript_vm_t* vm);
7172 static bool call_updater1(surgescript_object_t* object, void* updater);
7273 static bool call_updater2(surgescript_object_t* object, void* updater);
7374 static bool call_updater3(surgescript_object_t* object, void* updater);
8889 /* set up the VM */
8990 sslog("Creating the VM...");
9091 surgescript_var_init_pool();
91 create_vm_components(vm);
92 setup_sslib(vm);
92 init_vm(vm);
9393
9494 /* done! */
9595 return vm;
102102 surgescript_vm_t* surgescript_vm_destroy(surgescript_vm_t* vm)
103103 {
104104 sslog("Shutting down the VM...");
105 destroy_vm_components(vm);
105 release_vm(vm);
106106 surgescript_var_release_pool();
107107 return ssfree(vm);
108108 }
118118 if(surgescript_vm_is_active(vm)) {
119119 /* shut down */
120120 sslog("Shutting down the VM...");
121 destroy_vm_components(vm);
121 release_vm(vm);
122122 surgescript_var_release_pool();
123123
124124 /* set up the VM again */
125125 sslog("Starting the VM again...");
126126 surgescript_var_init_pool();
127 create_vm_components(vm);
128 setup_sslib(vm);
127 init_vm(vm);
129128
130129 /* done */
131130 return true;
193192
194193 /*
195194 * surgescript_vm_is_active()
196 * Is the VM active?
195 * Is the VM active? (i.e., turned ON)
197196 */
198197 bool surgescript_vm_is_active(surgescript_vm_t* vm)
199198 {
203202
204203 /*
205204 * surgescript_vm_update()
206 * Updates the VM
205 * Updates the VM. Returns true if the VM is active after this update cycle
207206 */
208207 bool surgescript_vm_update(surgescript_vm_t* vm)
209208 {
210 if(surgescript_vm_is_active(vm)) {
211 surgescript_object_t* root = surgescript_vm_root_object(vm);
212 surgescript_object_traverse_tree(root, surgescript_object_update);
213 return surgescript_vm_is_active(vm);
214 }
215 else
216 return false;
209 return surgescript_vm_update_ex(vm, NULL, NULL, NULL);
217210 }
218211
219212 /*
220213 * surgescript_vm_update_ex()
221 * Updates the VM, allowing user-defined callbacks as well
214 * Updates the VM, allowing user-defined callbacks as well.
215 * Returns true if the VM is active after this update cycle
222216 */
223217 bool surgescript_vm_update_ex(surgescript_vm_t* vm, void* user_data, void (*user_update)(surgescript_object_t*,void*), void (*late_update)(surgescript_object_t*,void*))
224218 {
225 if(surgescript_vm_is_active(vm)) {
219 if(surgescript_vm_is_active(vm) && !vm->is_paused) {
226220 surgescript_object_t* root = surgescript_vm_root_object(vm);
227221 surgescript_vm_updater_t updater = { user_data, user_update, late_update };
222
223 /* update time */
224 surgescript_vmtime_update(vm->time);
228225
229226 /* update */
230227 if(user_update != NULL && late_update != NULL)
239236 /* done! */
240237 return surgescript_vm_is_active(vm);
241238 }
242 else
243 return false;
239 else {
240 /* return true if the VM is still on */
241 return surgescript_vm_is_active(vm);
242 }
244243 }
245244
246245 /*
247246 * surgescript_vm_terminate()
248 * terminates the vm
247 * Terminates the vm
249248 */
250249 void surgescript_vm_terminate(surgescript_vm_t* vm)
251250 {
254253 }
255254
256255 /*
256 * surgescript_vm_pause()
257 * Pauses the VM, so that surgescript_vm_update_ex() does not update any objects
258 */
259 void surgescript_vm_pause(surgescript_vm_t* vm)
260 {
261 /* nothing to do */
262 if(vm->is_paused)
263 return;
264
265 /* pause the VM */
266 surgescript_vmtime_pause(vm->time);
267 vm->is_paused = true;
268 }
269
270 /*
271 * surgescript_vm_resume()
272 * Resumes a paused VM
273 */
274 void surgescript_vm_resume(surgescript_vm_t* vm)
275 {
276 /* nothing to do */
277 if(!vm->is_paused)
278 return;
279
280 /* pause the VM */
281 surgescript_vmtime_resume(vm->time);
282 vm->is_paused = false;
283 }
284
285 /*
286 * surgescript_vm_is_paused()
287 * Is the VM paused?
288 */
289 bool surgescript_vm_is_paused(const surgescript_vm_t* vm)
290 {
291 return vm->is_paused;
292 }
293
294 /*
257295 * surgescript_vm_programpool()
258296 * Gets the program pool
259297 */
293331 * surgescript_vm_args()
294332 * Gets the command-line arguments
295333 */
296 surgescript_vmargs_t* surgescript_vm_args(const surgescript_vm_t* vm)
334 const surgescript_vmargs_t* surgescript_vm_args(const surgescript_vm_t* vm)
297335 {
298336 return vm->args;
337 }
338
339 /*
340 * surgescript_vm_time()
341 * Gets the VM time
342 */
343 const surgescript_vmtime_t* surgescript_vm_time(const surgescript_vm_t* vm)
344 {
345 return vm->time;
299346 }
300347
301348 /*
352399
353400 /* ----- private ----- */
354401
355 /* creates the VM components */
356 void create_vm_components(surgescript_vm_t* vm)
357 {
358 vm->start_time = 0.0;
402 /* initializes the VM */
403 void init_vm(surgescript_vm_t* vm)
404 {
405 vm->is_paused = false;
406
407 /* create the VM components */
359408 vm->stack = surgescript_stack_create();
360409 vm->program_pool = surgescript_programpool_create();
361410 vm->tag_system = surgescript_tagsystem_create();
362411 vm->args = surgescript_vmargs_create();
363 vm->object_manager = surgescript_objectmanager_create(vm->program_pool, vm->tag_system, vm->stack, vm->args);
412 vm->time = surgescript_vmtime_create();
413 vm->object_manager = surgescript_objectmanager_create(vm->program_pool, vm->tag_system, vm->stack, vm->args, vm->time);
364414 vm->parser = surgescript_parser_create(vm->program_pool, vm->tag_system);
365 }
366
367 /* destroys the VM components */
368 void destroy_vm_components(surgescript_vm_t* vm)
369 {
370 surgescript_parser_destroy(vm->parser);
371 surgescript_objectmanager_destroy(vm->object_manager);
372 surgescript_vmargs_destroy(vm->args);
373 surgescript_tagsystem_destroy(vm->tag_system);
374 surgescript_programpool_destroy(vm->program_pool);
375 surgescript_stack_destroy(vm->stack);
376 }
377
378 /* load the SurgeScript library */
379 void setup_sslib(surgescript_vm_t* vm)
380 {
415
416 /* load the SurgeScript standard library */
381417 surgescript_sslib_register_object(vm);
382418 surgescript_sslib_register_string(vm);
383419 surgescript_sslib_register_number(vm);
398434 surgescript_sslib_register_system(vm);
399435 }
400436
437 /* releases the VM */
438 void release_vm(surgescript_vm_t* vm)
439 {
440 /* destroy the VM components */
441 surgescript_parser_destroy(vm->parser);
442 surgescript_objectmanager_destroy(vm->object_manager);
443 surgescript_vmtime_destroy(vm->time);
444 surgescript_vmargs_destroy(vm->args);
445 surgescript_tagsystem_destroy(vm->tag_system);
446 surgescript_programpool_destroy(vm->program_pool);
447 surgescript_stack_destroy(vm->stack);
448 }
449
401450 /* these auxiliary functions help traversing the object tree */
402451 bool call_updater1(surgescript_object_t* object, void* updater)
403452 {
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2018 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2018, 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
2121 #ifndef _SURGESCRIPT_RUNTIME_VM_H
2222 #define _SURGESCRIPT_RUNTIME_VM_H
2323
24 #include <stdint.h>
2425 #include <stdbool.h>
2526 #include "program.h"
2627 #include "object.h"
2728
2829 /* types */
2930 typedef struct surgescript_vm_t surgescript_vm_t;
30 struct surgescript_vmargs_t;
3131 struct surgescript_parser_t;
3232 struct surgescript_programpool_t;
33 struct surgescript_objectmanager_t;
3334 struct surgescript_tagsystem_t;
34 struct surgescript_objectmanager_t;
35 struct surgescript_vmargs_t;
36 struct surgescript_vmtime_t;
3537
3638 /* api */
3739 surgescript_vm_t* surgescript_vm_create();
4244 bool surgescript_vm_compile_code_in_memory(surgescript_vm_t* vm, const char* code); /* compiles the given code */
4345
4446 /* VM lifecycle */
45 bool surgescript_vm_is_active(surgescript_vm_t* vm); /* is the vm active? */
47 bool surgescript_vm_is_active(surgescript_vm_t* vm); /* is the vm active? (i.e., turned on) */
4648 void surgescript_vm_launch(surgescript_vm_t* vm); /* boots up the vm */
4749 void surgescript_vm_launch_ex(surgescript_vm_t* vm, int argc, char** argv); /* boots up the vm with command line arguments */
4850 void surgescript_vm_terminate(surgescript_vm_t* vm); /* terminates the vm */
51 bool surgescript_vm_reset(surgescript_vm_t* vm); /* resets the VM, clearing up all its programs and objects */
4952 bool surgescript_vm_update(surgescript_vm_t* vm); /* updates the vm */
5053 bool surgescript_vm_update_ex(surgescript_vm_t* vm, void* user_data, void (*user_update)(surgescript_object_t*,void*), void (*late_update)(surgescript_object_t*,void*)); /* updates the vm and allows more callbacks */
54 void surgescript_vm_pause(surgescript_vm_t* vm); /* pause the VM */
55 void surgescript_vm_resume(surgescript_vm_t* vm); /* resume a paused VM */
56 bool surgescript_vm_is_paused(const surgescript_vm_t* vm); /* is the VM paused? */
5157
5258 /* VM components */
5359 struct surgescript_programpool_t* surgescript_vm_programpool(const surgescript_vm_t* vm); /* gets the program pool */
5460 struct surgescript_tagsystem_t* surgescript_vm_tagsystem(const surgescript_vm_t* vm); /* gets the tag system */
5561 struct surgescript_objectmanager_t* surgescript_vm_objectmanager(const surgescript_vm_t* vm); /* gets the object manager */
5662 struct surgescript_parser_t* surgescript_vm_parser(const surgescript_vm_t* vm); /* gets the parser */
57 struct surgescript_vmargs_t* surgescript_vm_args(const surgescript_vm_t* vm); /* gets the command-line arguments */
63 const struct surgescript_vmargs_t* surgescript_vm_args(const surgescript_vm_t* vm); /* gets the command-line arguments */
64 const struct surgescript_vmtime_t* surgescript_vm_time(const surgescript_vm_t* vm); /* gets the VM time */
5865
5966 /* utilities */
6067 surgescript_object_t* surgescript_vm_root_object(surgescript_vm_t* vm); /* root object */
6269 surgescript_object_t* surgescript_vm_find_object(surgescript_vm_t* vm, const char* object_name); /* finds an object */
6370 void surgescript_vm_bind(surgescript_vm_t* vm, const char* object_name, const char* fun_name, surgescript_program_cfunction_t cfun, int num_params); /* binds a C function to an object */
6471 void surgescript_vm_install_plugin(surgescript_vm_t* vm, const char* object_name); /* sets a certain object as a plugin */
65 bool surgescript_vm_reset(surgescript_vm_t* vm); /* resets a VM, clearing up all its programs and objects */
6672
6773 #endif
0 /*
1 * SurgeScript
2 * A scripting language for games
3 * Copyright 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * runtime/vm_time.c
18 * SurgeScript Virtual Machine Time - this is used to count time
19 */
20
21 #include "vm_time.h"
22 #include "../util/util.h"
23
24 /* VM time */
25 struct surgescript_vmtime_t {
26 uint64_t time; /* in ms */
27 uint64_t ticks_at_last_update;
28 bool is_paused;
29 };
30
31 /*
32 * surgescript_vmtime_create()
33 * Create a VM time object
34 */
35 surgescript_vmtime_t* surgescript_vmtime_create()
36 {
37 surgescript_vmtime_t* vmtime = ssmalloc(sizeof *vmtime);
38
39 vmtime->time = 0;
40 vmtime->ticks_at_last_update = surgescript_util_gettickcount();
41 vmtime->is_paused = false;
42
43 return vmtime;
44 }
45
46 /*
47 * surgescript_vmtime_destroy()
48 * Destroy a VM time object
49 */
50 surgescript_vmtime_t* surgescript_vmtime_destroy(surgescript_vmtime_t* vmtime)
51 {
52 ssfree(vmtime);
53 return NULL;
54 }
55
56 /*
57 * surgescript_vmtime_update()
58 * Update the VM time object
59 */
60 void surgescript_vmtime_update(surgescript_vmtime_t* vmtime)
61 {
62 uint64_t now = surgescript_util_gettickcount();
63 uint64_t delta_time = now > vmtime->ticks_at_last_update ? now - vmtime->ticks_at_last_update : 0;
64 vmtime->time += vmtime->is_paused ? 0 : delta_time;
65 vmtime->ticks_at_last_update = now;
66 }
67
68 /*
69 * surgescript_vmtime_pause()
70 * Pause the VM time
71 */
72 void surgescript_vmtime_pause(surgescript_vmtime_t* vmtime)
73 {
74 /* nothing to do */
75 if(vmtime->is_paused)
76 return;
77
78 /* pause the time */
79 vmtime->is_paused = true;
80 }
81
82 /*
83 * surgescript_vmtime_resume()
84 * Resume the VM time
85 */
86 void surgescript_vmtime_resume(surgescript_vmtime_t* vmtime)
87 {
88 /* nothing to do */
89 if(!vmtime->is_paused)
90 return;
91
92 /* resume the time */
93 vmtime->ticks_at_last_update = surgescript_util_gettickcount();
94 vmtime->is_paused = false;
95 }
96
97 /*
98 * surgescript_vmtime_time()
99 * Get the time, in milliseconds, at the beginning of the current update cycle
100 */
101 uint64_t surgescript_vmtime_time(const surgescript_vmtime_t* vmtime)
102 {
103 return vmtime->time;
104 }
105
106 /*
107 * surgescript_vmtime_is_paused()
108 * Is the VM time paused?
109 */
110 bool surgescript_vmtime_is_paused(const surgescript_vmtime_t* vmtime)
111 {
112 return vmtime->is_paused;
113 }
0 /*
1 * SurgeScript
2 * A scripting language for games
3 * Copyright 2021 Alexandre Martins <alemartf(at)gmail(dot)com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * runtime/vm_time.h
18 * SurgeScript Virtual Machine Time - this is used to count time
19 */
20
21 #ifndef _SURGESCRIPT_RUNTIME_VM_TIME_H
22 #define _SURGESCRIPT_RUNTIME_VM_TIME_H
23
24 #include <stdint.h>
25 #include <stdbool.h>
26
27 typedef struct surgescript_vmtime_t surgescript_vmtime_t;
28
29 surgescript_vmtime_t* surgescript_vmtime_create(); /* create a VM time object */
30 surgescript_vmtime_t* surgescript_vmtime_destroy(surgescript_vmtime_t* vmtime); /* destroy a VM time object */
31
32 void surgescript_vmtime_update(surgescript_vmtime_t* vmtime); /* update the VM time object */
33 void surgescript_vmtime_pause(surgescript_vmtime_t* vmtime); /* pause the VM time */
34 void surgescript_vmtime_resume(surgescript_vmtime_t* vmtime); /* resume the VM time */
35
36 uint64_t surgescript_vmtime_time(const surgescript_vmtime_t* vmtime); /* the time at the beginning of the current update cycle */
37 bool surgescript_vmtime_is_paused(const surgescript_vmtime_t* vmtime); /* is the VM time paused? */
38
39 #endif
2727 * SSARRAY()
2828 * declares an array of a certain type
2929 */
30 #define SSARRAY(type, arr) type* arr; size_t arr##_len, arr##_cap;
30 #define SSARRAY(type, arr) type* arr; size_t arr##_len, arr##_cap
3131
3232 /*
3333 * ssarray_init()
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2020 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
3434 #endif
3535
3636 /* private stuff */
37 static void mem_crash(const char* location);
37 static void mem_crash(const char* file, int line);
3838 static void my_log(const char* message);
3939 static void my_fatal(const char* message);
4040 static void (*log_function)(const char* message) = my_log;
5050 * surgescript_util_malloc()
5151 * Memory allocation routine
5252 */
53 void* surgescript_util_malloc(size_t bytes, const char* location)
53 void* surgescript_util_malloc(size_t bytes, const char* file, int line)
5454 {
5555 void *m = malloc(bytes);
5656
5757 if(m == NULL)
58 mem_crash(location);
58 mem_crash(file, line);
5959
6060 return m;
6161 }
6464 * surgescript_util_realloc()
6565 * Memory reallocation routine
6666 */
67 void* surgescript_util_realloc(void* ptr, size_t bytes, const char* location)
67 void* surgescript_util_realloc(void* ptr, size_t bytes, const char* file, int line)
6868 {
6969 void *m = realloc(ptr, bytes);
7070
7171 if(m == NULL)
72 mem_crash(location);
72 mem_crash(file, line);
7373
7474 return m;
7575 }
8080 */
8181 void* surgescript_util_free(void* ptr)
8282 {
83 if(ptr)
83 if(ptr != NULL)
8484 free(ptr);
8585
8686 return NULL;
219219 * surgescript_util_strdup()
220220 * Copies a string into another, allocating the required memory
221221 */
222 char* surgescript_util_strdup(const char* src, const char* location)
223 {
224 char* str = surgescript_util_malloc(sizeof(char) * (1 + strlen(src)), location);
222 char* surgescript_util_strdup(const char* src, const char* file, int line)
223 {
224 char* str = surgescript_util_malloc(sizeof(char) * (1 + strlen(src)), file, line);
225225 return strcpy(str, src);
226226 }
227227
377377 fprintf(stderr, "%s\n", message);
378378 }
379379
380 void mem_crash(const char* location) /* out of memory error */
381 {
382 static char buf[128] = "Out of memory in ";
383 surgescript_util_strncpy(buf + 17, location, sizeof(buf) - 17);
380 void mem_crash(const char* file, int line) /* out of memory error */
381 {
382 static char buf[1024] = "Out of memory in ";
383 static const int prefix_len = 17;
384
385 snprintf(buf + prefix_len, sizeof(buf) - prefix_len, "%s:%d", file, line);
384386 fatal_function(buf);
387
385388 exit(1); /* just in case */
386 }
389 }
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2020 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
3232 #define sssign(x) ((x) >= 0 ? 1 : -1)
3333 #define sstok(x) #x
3434 #define ssstr(x) sstok(x)
35 #define ssassert(expr) do { if(!(expr)) ssfatal("%s", "In " __FILE__ ":" ssstr(__LINE__) ": assertion `" sstok(expr) "` failed."); } while(0)
35 #define ssassert(expr) do { if(!(expr)) ssfatal("In %s:%d: %s", __FILE__, __LINE__, ": assertion `" sstok(expr) "` failed."); } while(0)
3636
3737 /* common aliases */
38 #define ssmalloc(n) surgescript_util_malloc((n), __FILE__ ":" ssstr(__LINE__))
39 #define ssrealloc(p, n) surgescript_util_realloc((p), (n), __FILE__ ":" ssstr(__LINE__))
38 #define ssmalloc(n) surgescript_util_malloc((n), __FILE__, __LINE__)
39 #define ssrealloc(p, n) surgescript_util_realloc((p), (n), __FILE__, __LINE__)
4040 #define ssfree surgescript_util_free
4141 #define sslog surgescript_util_log
4242 #define ssfatal surgescript_util_fatal
43 #define ssstrdup(str) surgescript_util_strdup((str), __FILE__ ":" ssstr(__LINE__))
43 #define ssstrdup(str) surgescript_util_strdup((str), __FILE__, __LINE__)
4444
4545 /* constants */
4646 #define SS_NAMEMAX 63 /* names can't be larger than this (computes hashes quickly) */
5252 const char* surgescript_util_website(); /* project website */
5353 const char* surgescript_util_authors(); /* project authors */
5454
55 void* surgescript_util_malloc(size_t bytes, const char* location); /* memory allocation */
56 void* surgescript_util_realloc(void* ptr, size_t bytes, const char* location); /* memory reallocation */
55 void* surgescript_util_malloc(size_t bytes, const char* file, int line); /* memory allocation */
56 void* surgescript_util_realloc(void* ptr, size_t bytes, const char* file, int line); /* memory reallocation */
5757 void* surgescript_util_free(void* ptr); /* memory deallocation */
5858
5959 void surgescript_util_log(const char* fmt, ...); /* logs a message */
6161 void surgescript_util_set_error_functions(void (*log)(const char*), void (*fatal)(const char*)); /* set custom error functions */
6262
6363 char* surgescript_util_strncpy(char* dst, const char* src, size_t n); /* strcpy */
64 char* surgescript_util_strdup(const char* src, const char* location); /* strdup */
64 char* surgescript_util_strdup(const char* src, const char* file, int line); /* strdup */
6565 const char* surgescript_util_basename(const char* path); /* basename */
6666 char* surgescript_util_accessorfun(const char* prefix, const char* text); /* getter/setter prefixing function */
6767
0 /*
1 * SurgeScript
2 * A scripting language for games
3 * Copyright 2016-2021 Alexandre Martins <alemartf(at)gmail(dot)com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * util/version.h
18 * SurgeScript version: available at compile time
19 */
20
21 #ifndef _SURGESCRIPT_VERSION_H
22 #define _SURGESCRIPT_VERSION_H
23
24 /* Import SurgeScript version */
25 #define SURGESCRIPT_VERSION_SUP @PROJECT_VERSION_MAJOR@
26 #define SURGESCRIPT_VERSION_SUB @PROJECT_VERSION_MINOR@
27 #define SURGESCRIPT_VERSION_WIP @PROJECT_VERSION_PATCH@
28 #cmakedefine SURGESCRIPT_VERSION_FIX @PROJECT_VERSION_TWEAK@
29
30 #if !defined(SURGESCRIPT_VERSION_FIX)
31 #define SURGESCRIPT_VERSION_FIX 0
32 #endif
33
34 /* Generate version string */
35 #define SURGESCRIPT_VERSION_STR "@PROJECT_VERSION@"
36
37 /* Convert a version tuple into an integer */
38 #define SURGESCRIPT_VERSION_CODE(x, y, z, w) \
39 (((x) << 24) | ((y) << 16) | ((z) << 8) | (w))
40
41 /* Compare a version tuple to the current version of SurgeScript */
42 #define SURGESCRIPT_VERSION_COMPARE(x, y, z, w) ( \
43 SURGESCRIPT_VERSION_CODE( \
44 SURGESCRIPT_VERSION_SUP, \
45 SURGESCRIPT_VERSION_SUB, \
46 SURGESCRIPT_VERSION_WIP, \
47 SURGESCRIPT_VERSION_FIX \
48 ) - \
49 SURGESCRIPT_VERSION_CODE((x), (y), (z), (w)) \
50 )
51
52 /* Check if the current version of SurgeScript is x.y.z.w or newer */
53 #define SURGESCRIPT_VERSION_IS_AT_LEAST(x, y, z, w) \
54 (SURGESCRIPT_VERSION_COMPARE((x), (y), (z), (w)) >= 0)
55
56 #endif
00 /*
11 * SurgeScript
22 * A scripting language for games
3 * Copyright 2016-2020 Alexandre Martins <alemartf(at)gmail(dot)com>
3 * Copyright 2016-2021 Alexandre Martins <alemartf(at)gmail(dot)com>
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
2929 #include "surgescript/runtime/program.h"
3030 #include "surgescript/runtime/object.h"
3131 #include "surgescript/runtime/program_pool.h"
32 #include "surgescript/runtime/object_manager.h"
3233 #include "surgescript/runtime/tag_system.h"
33 #include "surgescript/runtime/object_manager.h"
34 #include "surgescript/runtime/vm_time.h"
3435 #include "surgescript/runtime/heap.h"
3536 #include "surgescript/runtime/stack.h"
3637 #include "surgescript/runtime/variable.h"
3839 #include "surgescript/util/transform.h"
3940 #include "surgescript/util/ssarray.h"
4041 #include "surgescript/util/util.h"
42 #include "surgescript/util/version.h"
4143
4244 #ifdef __cplusplus
4345 }
4446 #endif
4547
46 #endif
48 #endif