Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions json/game_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"backgroundColor.b": 235,
"backgroundColor.g": 206,
"backgroundColor.r": 135,
"gameTitle": "My SplashKit Game",
"windowHeight": 600,
"windowWidth": 800
}
60 changes: 60 additions & 0 deletions public/usage-examples/json/json_from_file-1-example-oop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

class JsonConfigLoadExample
{
static void Main()
{
// Initialize audio system
OpenAudio();

// First, create a sample configuration file
dynamic config = CreateJson();
JsonSetString(config, "gameTitle", "My SplashKit Game");
JsonSetNumber(config, "windowWidth", 800);
JsonSetNumber(config, "windowHeight", 600);
JsonSetNumber(config, "backgroundColor.r", 135);
JsonSetNumber(config, "backgroundColor.g", 206);
JsonSetNumber(config, "backgroundColor.b", 235);

// Save the configuration to a file
JsonToFile(config, "game_config.json");

// Now demonstrate loading it back
// This is what json_from_file does
dynamic loadedConfig = JsonFromFile("game_config.json");

// Extract the configuration values
string gameTitle = JsonReadString(loadedConfig, "gameTitle");
double windowWidth = JsonReadNumber(loadedConfig, "windowWidth");
double windowHeight = JsonReadNumber(loadedConfig, "windowHeight");
int bgRed = (int)JsonReadNumber(loadedConfig, "backgroundColor.r");
int bgGreen = (int)JsonReadNumber(loadedConfig, "backgroundColor.g");
int bgBlue = (int)JsonReadNumber(loadedConfig, "backgroundColor.b");

// Create window with loaded dimensions
OpenWindow(gameTitle, (int)windowWidth, (int)windowHeight);

// Create a color from the loaded RGB values
Color backgroundColor = RGBColor(bgRed, bgGreen, bgBlue);

// Display the loaded configuration
ClearScreen(backgroundColor);

DrawText("Configuration Loaded Successfully!", ColorBlack(), 50, 50);
DrawText("Game Title: " + gameTitle, ColorBlack(), 50, 100);
DrawText("Window Size: " + windowWidth + "x" + windowHeight, ColorBlack(), 50, 150);
DrawText("Background Color: RGB(" + bgRed + ", " + bgGreen + ", " + bgBlue + ")", ColorBlack(), 50, 200);

RefreshScreen();

// Wait 3 seconds before closing
Delay(3000);

// Cleanup
CloseAllWindows();
FreeJson(config);
FreeJson(loadedConfig);
CloseAudio();
}
}
60 changes: 60 additions & 0 deletions public/usage-examples/json/json_from_file-1-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "splashkit.h"
#include <iostream>

int main()
{
// Initialize audio system
open_audio();

// First, create a sample configuration file
// This would typically already exist in your project
json config = create_json();
json_set_string(config, "gameTitle", "My SplashKit Game");
json_set_number(config, "windowWidth", 800);
json_set_number(config, "windowHeight", 600);
json_set_number(config, "backgroundColor.r", 135);
json_set_number(config, "backgroundColor.g", 206);
json_set_number(config, "backgroundColor.b", 235);

// Save the configuration to a file
json_to_file(config, "game_config.json");

// Now demonstrate loading it back
// This is what json_from_file does
json loaded_config = json_from_file("game_config.json");

// Extract the configuration values
string game_title = json_read_string(loaded_config, "gameTitle");
double window_width = json_read_number(loaded_config, "windowWidth");
double window_height = json_read_number(loaded_config, "windowHeight");
int bg_red = json_read_number(loaded_config, "backgroundColor.r");
int bg_green = json_read_number(loaded_config, "backgroundColor.g");
int bg_blue = json_read_number(loaded_config, "backgroundColor.b");

// Create window with loaded dimensions
open_window(game_title, window_width, window_height);

// Create a color from the loaded RGB values
color background_color = rgb_color(bg_red, bg_green, bg_blue);

// Display the loaded configuration
clear_screen(background_color);

draw_text("Configuration Loaded Successfully!", COLOR_BLACK, 50, 50);
draw_text("Game Title: " + game_title, COLOR_BLACK, 50, 100);
draw_text("Window Size: " + to_string((int)window_width) + "x" + to_string((int)window_height), COLOR_BLACK, 50, 150);
draw_text("Background Color: RGB(" + to_string(bg_red) + ", " + to_string(bg_green) + ", " + to_string(bg_blue) + ")", COLOR_BLACK, 50, 200);

refresh_screen();

// Wait 3 seconds before closing
delay(3000);

// Cleanup
close_all_windows();
free_json(config);
free_json(loaded_config);
close_audio();

return 0;
}
53 changes: 53 additions & 0 deletions public/usage-examples/json/json_from_file-1-example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using SplashKitSDK;

// Initialize audio system
SplashKit.OpenAudio();

// First, create a sample configuration file
dynamic config = SplashKit.CreateJson();
SplashKit.JsonSetString(config, "gameTitle", "My SplashKit Game");
SplashKit.JsonSetNumber(config, "windowWidth", 800);
SplashKit.JsonSetNumber(config, "windowHeight", 600);
SplashKit.JsonSetNumber(config, "backgroundColor.r", 135);
SplashKit.JsonSetNumber(config, "backgroundColor.g", 206);
SplashKit.JsonSetNumber(config, "backgroundColor.b", 235);

// Save the configuration to a file
SplashKit.JsonToFile(config, "game_config.json");

// Now demonstrate loading it back
// This is what json_from_file does
dynamic loadedConfig = SplashKit.JsonFromFile("game_config.json");

// Extract the configuration values
string gameTitle = SplashKit.JsonReadString(loadedConfig, "gameTitle");
double windowWidth = SplashKit.JsonReadNumber(loadedConfig, "windowWidth");
double windowHeight = SplashKit.JsonReadNumber(loadedConfig, "windowHeight");
int bgRed = (int)SplashKit.JsonReadNumber(loadedConfig, "backgroundColor.r");
int bgGreen = (int)SplashKit.JsonReadNumber(loadedConfig, "backgroundColor.g");
int bgBlue = (int)SplashKit.JsonReadNumber(loadedConfig, "backgroundColor.b");

// Create window with loaded dimensions
SplashKit.OpenWindow(gameTitle, (int)windowWidth, (int)windowHeight);

// Create a color from the loaded RGB values
Color backgroundColor = SplashKit.RGBColor(bgRed, bgGreen, bgBlue);

// Display the loaded configuration
SplashKit.ClearScreen(backgroundColor);

SplashKit.DrawText("Configuration Loaded Successfully!", SplashKit.ColorBlack(), 50, 50);
SplashKit.DrawText("Game Title: " + gameTitle, SplashKit.ColorBlack(), 50, 100);
SplashKit.DrawText("Window Size: " + windowWidth + "x" + windowHeight, SplashKit.ColorBlack(), 50, 150);
SplashKit.DrawText("Background Color: RGB(" + bgRed + ", " + bgGreen + ", " + bgBlue + ")", SplashKit.ColorBlack(), 50, 200);

SplashKit.RefreshScreen();

// Wait 3 seconds before closing
SplashKit.Delay(3000);

// Cleanup
SplashKit.CloseAllWindows();
SplashKit.FreeJson(config);
SplashKit.FreeJson(loadedConfig);
SplashKit.CloseAudio();
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions public/usage-examples/json/json_from_file-1-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from splashkit import *

# Initialize audio system
open_audio()

# First, create a sample configuration file
config = create_json()
json_set_string(config, "gameTitle", "My SplashKit Game")
json_set_number_integer(config, "windowWidth", 800)
json_set_number_integer(config, "windowHeight", 600)
json_set_number_integer(config, "backgroundColor.r", 135)
json_set_number_integer(config, "backgroundColor.g", 206)
json_set_number_integer(config, "backgroundColor.b", 235)

# Save the configuration to a file
json_to_file(config, "game_config.json")

# Now demonstrate loading it back
# This is what json_from_file does
loaded_config = json_from_file("game_config.json")

# Extract the configuration values
game_title = json_read_string(loaded_config, "gameTitle")
window_width = json_read_number(loaded_config, "windowWidth")
window_height = json_read_number(loaded_config, "windowHeight")
bg_red = int(json_read_number(loaded_config, "backgroundColor.r"))
bg_green = int(json_read_number(loaded_config, "backgroundColor.g"))
bg_blue = int(json_read_number(loaded_config, "backgroundColor.b"))

# Create window with loaded dimensions
open_window(game_title, int(window_width), int(window_height))

# Create a color from the loaded RGB values
background_color = rgb_color(bg_red, bg_green, bg_blue)

# Display the loaded configuration
clear_screen(background_color)

draw_text_no_font_no_size("Configuration Loaded Successfully!", color_black(), 50, 50)
draw_text_no_font_no_size(f"Game Title: {game_title}", color_black(), 50, 100)
draw_text_no_font_no_size(f"Window Size: {int(window_width)}x{int(window_height)}", color_black(), 50, 150)
draw_text_no_font_no_size(f"Background Color: RGB({bg_red}, {bg_green}, {bg_blue})", color_black(), 50, 200)

refresh_screen()

# Wait 3 seconds before closing
delay(3000)

# Cleanup
close_all_windows()
free_json(config)
free_json(loaded_config)
close_audio()
5 changes: 5 additions & 0 deletions public/usage-examples/json/json_from_file-1-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Loading Game Configuration from JSON

This example demonstrates how to load game settings from a JSON configuration file.
It shows how to read various data types from JSON (strings, numbers) and apply them
to configure the game window and display values.