diff --git a/json/game_config.json b/json/game_config.json new file mode 100644 index 000000000..728ebbcf7 --- /dev/null +++ b/json/game_config.json @@ -0,0 +1,8 @@ +{ + "backgroundColor.b": 235, + "backgroundColor.g": 206, + "backgroundColor.r": 135, + "gameTitle": "My SplashKit Game", + "windowHeight": 600, + "windowWidth": 800 +} \ No newline at end of file diff --git a/public/usage-examples/json/json_from_file-1-example-oop.cs b/public/usage-examples/json/json_from_file-1-example-oop.cs new file mode 100644 index 000000000..e55ed3cb9 --- /dev/null +++ b/public/usage-examples/json/json_from_file-1-example-oop.cs @@ -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(); + } +} \ No newline at end of file diff --git a/public/usage-examples/json/json_from_file-1-example.cpp b/public/usage-examples/json/json_from_file-1-example.cpp new file mode 100644 index 000000000..27c276a3a --- /dev/null +++ b/public/usage-examples/json/json_from_file-1-example.cpp @@ -0,0 +1,60 @@ +#include "splashkit.h" +#include + +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; +} \ No newline at end of file diff --git a/public/usage-examples/json/json_from_file-1-example.cs b/public/usage-examples/json/json_from_file-1-example.cs new file mode 100644 index 000000000..985e477ff --- /dev/null +++ b/public/usage-examples/json/json_from_file-1-example.cs @@ -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(); \ No newline at end of file diff --git a/public/usage-examples/json/json_from_file-1-example.png b/public/usage-examples/json/json_from_file-1-example.png new file mode 100644 index 000000000..8c57a42aa Binary files /dev/null and b/public/usage-examples/json/json_from_file-1-example.png differ diff --git a/public/usage-examples/json/json_from_file-1-example.py b/public/usage-examples/json/json_from_file-1-example.py new file mode 100644 index 000000000..b69fee2c9 --- /dev/null +++ b/public/usage-examples/json/json_from_file-1-example.py @@ -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() \ No newline at end of file diff --git a/public/usage-examples/json/json_from_file-1-example.txt b/public/usage-examples/json/json_from_file-1-example.txt new file mode 100644 index 000000000..227362b91 --- /dev/null +++ b/public/usage-examples/json/json_from_file-1-example.txt @@ -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. \ No newline at end of file