Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions public/usage-examples/json/data_driven_dungeon-1-example-oop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

namespace DataDrivenDungeon
{
public class DungeonManager
{
private Json _levelData;

public DungeonManager(string fileName)
{
_levelData = JsonFromFile(fileName);
}

public string LevelName => JsonReadString(_levelData, "level_name");
public int Difficulty => JsonReadInteger(_levelData, "difficulty");
public string Theme => JsonReadString(_levelData, "theme");

public void Draw()
{
ClearScreen(ColorBlack());
DrawText($"Level: {LevelName}", ColorWhite(), 20, 20);
DrawText($"Difficulty: {Difficulty}", ColorWhite(), 20, 40);
DrawText($"Theme: {Theme}", ColorWhite(), 20, 60);

// Accessing the 'spawn_points' array and looping through objects
Json spawnPoints = JsonReadArray(_levelData, "spawn_points");
for (int i = 0; i < JsonArraySize(spawnPoints); i++)
{
Json entry = JsonReadObjectAtIndex(spawnPoints, i);
string entryType = JsonReadString(entry, "type");
double x = JsonReadNumber(entry, "x");
double y = JsonReadNumber(entry, "y");

Color drawColor = (entryType == "Player") ? ColorGreen() : ColorRed();
FillRectangle(drawColor, x, y, 32, 32);
DrawText(entryType, ColorWhite(), (float)x, (float)y - 15);
}

// Accessing the 'loot' array
Json lootPoints = JsonReadArray(_levelData, "loot");
for (int i = 0; i < JsonArraySize(lootPoints); i++)
{
Json item = JsonReadObjectAtIndex(lootPoints, i);
string lootName = JsonReadString(item, "item");
double x = JsonReadNumber(item, "x");
double y = JsonReadNumber(item, "y");

FillCircle(ColorGold(), (float)x, (float)y, 8);
DrawText(lootName, ColorGold(), (float)x - 20, (float)y - 20);
}
}

public void Cleanup()
{
FreeJson(_levelData);
}
}

public class Program
{
public static void Main()
{
DungeonManager dungeon = new DungeonManager("level_data.json");
OpenWindow($"Dungeon Navigator: {dungeon.LevelName}", 800, 600);

while (!QuitRequested())
{
ProcessEvents();
dungeon.Draw();
RefreshScreen(60);
}

dungeon.Cleanup();
CloseAllWindows();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Data-Driven dungeon loading logic
Json levelInfo = JsonFromFile("level_data.json");

// Access strings and ints
string levelName = JsonReadString(levelInfo, "level_name");
int difficulty = JsonReadInteger(levelInfo, "difficulty");
string theme = JsonReadString(levelInfo, "theme");

OpenWindow($"Dungeon: {levelName}", 800, 600);

while (!QuitRequested())
{
ProcessEvents();
ClearScreen(ColorBlack());

DrawText($"Level Name: {levelName}", ColorWhite(), 20, 20);
DrawText($"Difficulty: {difficulty}", ColorWhite(), 20, 40);
DrawText($"Theme: {theme}", ColorWhite(), 20, 60);

// Reading nested objects from an array
Json spawnPoints = JsonReadArray(levelInfo, "spawn_points");
for (int i = 0; i < JsonArraySize(spawnPoints); i++)
{
Json entry = JsonReadObjectAtIndex(spawnPoints, i);
string type = JsonReadString(entry, "type");
double x = JsonReadNumber(entry, "x");
double y = JsonReadNumber(entry, "y");

Color drawColor = (type == "Player") ? ColorGreen() : ColorRed();
FillRectangle(drawColor, x, y, 32, 32);
DrawText(type, ColorWhite(), (float)x, (float)y - 15);
}

// Reading loot points
Json lootItems = JsonReadArray(levelInfo, "loot");
for (int i = 0; i < JsonArraySize(lootItems); i++)
{
Json item = JsonReadObjectAtIndex(lootItems, i);
string itemName = JsonReadString(item, "item");
double x = JsonReadNumber(item, "x");
double y = JsonReadNumber(item, "y");

FillCircle(ColorGold(), (float)x, (float)y, 8);
DrawText(itemName, ColorGold(), (float)x - 20, (float)y - 20);
}

RefreshScreen(60);
}

FreeJson(levelInfo);
CloseAllWindows();
70 changes: 70 additions & 0 deletions public/usage-examples/json/data_driven_dungeon-1-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "splashkit.h"

int main()
{
// Load the JSON file directly
json level_info = json_from_file("public/usage-examples/json/level_data.json");

// Check if JSON loaded successfully
if (!json_has_key(level_info, "level_name"))
{
// Fallback for local execution if path above fails
free_json(level_info);
level_info = json_from_file("level_data.json");
}

// Reading top-level strings and integers
string level_name = json_read_string(level_info, "level_name");
int difficulty = json_read_number_as_int(level_info, "difficulty");
string theme = json_read_string(level_info, "theme");

open_window("Data-Driven Dungeon: " + level_name, 800, 600);

while (!quit_requested())
{
process_events();
clear_screen(color_black());

// Display Dungeon metadata
draw_text("Level: " + level_name, color_white(), 20, 20);
draw_text("Difficulty: " + std::to_string(difficulty), color_white(), 20, 40);
draw_text("Theme: " + theme, color_white(), 20, 60);

// Accessing the 'spawn_points' array and looping through objects
vector<json> spawn_points;
json_read_array(level_info, "spawn_points", spawn_points);
for (int i = 0; i < spawn_points.size(); i++)
{
json entry = spawn_points[i];
string type = json_read_string(entry, "type");
double x = json_read_number(entry, "x");
double y = json_read_number(entry, "y");

color draw_color = (type == "Player") ? color_green() : color_red();
fill_rectangle(draw_color, x, y, 32, 32);
draw_text(type, color_white(), x, y - 15);
}

// Accessing the 'loot' array
vector<json> loot_list;
json_read_array(level_info, "loot", loot_list);
for (int i = 0; i < loot_list.size(); i++)
{
json item = loot_list[i];
string item_name = json_read_string(item, "item");
double x = json_read_number(item, "x");
double y = json_read_number(item, "y");

fill_circle(color_gold(), x, y, 8);
draw_text(item_name, color_gold(), x - 20, y - 20);
}

refresh_screen(60);
}

// Clean up
free_json(level_info);
close_all_windows();

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions public/usage-examples/json/data_driven_dungeon-1-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from splashkit import *

def main():
# Load the JSON file
level_info = json_from_file("level_data.json")

# Read basic data fields
level_name = json_read_string(level_info, "level_name")
difficulty = json_read_integer(level_info, "difficulty")
theme = json_read_string(level_info, "theme")

open_window(f"Data-Driven Dungeon: {level_name}", 800, 600)

while not quit_requested():
process_events()
clear_screen(color_black())

# Display metadata on screen
draw_text(f"Level Name: {level_name}", color_white(), 20, 20)
draw_text(f"Difficulty Score: {difficulty}", color_white(), 20, 40)
draw_text(f"Dungeon Theme: {theme}", color_white(), 20, 60)

# Access arrays and objects nested inside the JSON
spawn_points = json_read_array(level_info, "spawn_points")
for i in range(json_array_size(spawn_points)):
entry = json_read_object_at_index(spawn_points, i)
entity_type = json_read_string(entry, "type")
x = json_read_number(entry, "x")
y = json_read_number(entry, "y")

draw_color = color_green() if entity_type == "Player" else color_red()
fill_rectangle(draw_color, x, y, 32, 32)
draw_text(entity_type, color_white(), x, y - 15)

# Iterate through loot array
loot_items = json_read_array(level_info, "loot")
for i in range(json_array_size(loot_items)):
item = json_read_object_at_index(loot_items, i)
item_name = json_read_string(item, "item")
x = json_read_number(item, "x")
y = json_read_number(item, "y")

fill_circle(color_gold(), x, y, 8)
draw_text(item_name, color_gold(), x - 20, y - 20)

refresh_screen(60)

# Note: free_json(level_info) is implicitly handled in splashkit-python wrapper but explicit cleanup is good practice
close_all_windows()

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Data-Driven Dungeon Integration Example
16 changes: 16 additions & 0 deletions public/usage-examples/json/level_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"level_name": "Skeleton Crypt",
"difficulty": 2,
"theme": "Gothic",
"dungeon_width": 800,
"dungeon_height": 600,
"spawn_points": [
{"type": "Player", "x": 100, "y": 100},
{"type": "Skeleton", "x": 400, "y": 300},
{"type": "Skeleton", "x": 600, "y": 450}
],
"loot": [
{"item": "Health Potion", "x": 150, "y": 120},
{"item": "Rusty Key", "x": 750, "y": 50}
]
}
64 changes: 64 additions & 0 deletions scripts/json-files/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -53363,6 +53363,38 @@
"brief": "SplashKit Json allows you to create and read JSON objects.",
"description": "Splashkit's JSON library allows you to easily create or read JSON objects and\nmanipulate them to/from a JSON string or from a file containing a JSON\nstring. Create a new JSON object with a call to `create_json()` and\nread or write data to it by calling methods like\n`json_add_string(json j, string key, string value)` and\n`json_read_string(json j, string key)`.",
"functions": [
{
"signature": "json data_driven_dungeon();",
"name": "data_driven_dungeon",
"description": "Demonstrates loading a dungeon level from a JSON file, including parsing arrays of objects for spawn points and loot items.",
"brief": "Data-Driven Dungeon Integration Example",
"unique_global_name": "data_driven_dungeon",
"group": "json",
"static": "json",
"parameters": {},
"return": {
"type": "json",
"description": "Returns a json object.",
"is_pointer": false,
"is_reference": false,
"is_vector": false,
"type_parameter": null
},
"attributes": {
"group": "json",
"static": "json"
},
"signatures": {
"python": [
"def data_driven_dungeon();"
],
"cpp": [
"json data_driven_dungeon()"
],
"csharp": [],
"pascal": []
}
},
{
"signature": "json create_json();",
"name": "create_json",
Expand Down Expand Up @@ -63274,6 +63306,38 @@
"brief": "SplashKit Collisions library allow you to perform tests between\nbitmaps, sprites and shapes to determin if a collision has occured.Provides matrix functions to work on 2d coordinates.Provides vector functions to work on vectors.",
"description": "",
"functions": [
{
"signature": "void celestial_mechanics();",
"name": "celestial_mechanics",
"description": "Orbital mechanics example demonstrating N-body gravity simulation with planetary bodies.",
"brief": "Celestial Mechanics Integration Example",
"unique_global_name": "celestial_mechanics",
"group": "physics",
"static": "physics",
"parameters": {},
"return": {
"type": "void",
"description": "N/A",
"is_pointer": false,
"is_reference": false,
"is_vector": false,
"type_parameter": null
},
"attributes": {
"group": "physics",
"static": "physics"
},
"signatures": {
"python": [
"def celestial_mechanics():"
],
"cpp": [
"void celestial_mechanics()"
],
"csharp": [],
"pascal": []
}
},
{
"signature": "bool bitmap_circle_collision(bitmap bmp,const point_2d &pt,const circle &circ);",
"name": "bitmap_circle_collision",
Expand Down