What is Splashkit?

What is Splashkit?

Unleashing the Power of SplashKit: A Beginner's Guide to Creating Games and Animations with C++

Splashkit is one of the c++ language libraries created by an Australian professor 'Andrew Cain', this is an open-source Software Development Kit(SDK), which is very fascinating and easy to understand for beginners in c++. With the help of many inbuilt functions in this, one can create many games, animations, scenarios, etc. In this blog, we will explore how to use SplashKit to create simple games and animations, and we will provide tips and best practices for getting the most out of this powerful tool.

How it is better than the <iostream>

Everyone reading this blog must have been familiar with iostream, if not I would explain what it is. "iostream" is a header file in C++ that provides a standard set of functions for performing input and output operations, such as reading from and writing to files and the console. iostream is commonly used by C/C++ developers for a wide range of input/output tasks. Further, we will also be comparing the syntax differences between the iostream and splashkit too. But for now, let's stick to why splashkit is better than iostream. The following are the positive points that make it better:

  • Reduces the overhead required for technical coding

  • This toolkit help supercharge our programs while we are learning

  • This works cross-platform and across multiple programming languages

  • You may access the information about the list of many coding artefacts but nevertheless, we will still be studying them in this blog, as it goes on.

Setting up Splashkit on Windows PC

  • To get started from basic, you need to install MSYS2 terminal on your computer. You will find instructions to download it here MSYS2

  • After installing MSYS2 terminal successfully you will have to install Splashkit Library on your computer. Start it with installing git client. pacman -S git --noconfirm --disable-download-timeout copy paste this command in your terminal and run it.

  • Then cop paste this command bash <(curl -s raw.githubusercontent.com/splashkit/skm/mas..) on the terminal to complete the installation of Splashkit library.

  • Now to install gcc on windows you may run this command on terminal pacman --disable-download-timeout -S mingw-w64-{x86_64,i686}-gcc mingw-w64-{i686,x86_64}-gdb

  • Now you are all set to start, and just need an editor , you may use any that you want.

    For macOS, please refer to the following guide: https://splashkit.io/articles/installation/mac/

    Tutorial: Building a Simple Game with SplashKit

    In this tutorial, you'll learn how to use SplashKit to build a simple game. By the end of this tutorial, you'll be able to:

    • Set up a SplashKit project

    • Draw shapes and images to the screen

    • Respond to user input

To complete this tutorial, you'll need to have SplashKit installed on your computer. If you don't have SplashKit installed yet, you can follow the instructions as described above to install it.

#### Step 1: Create a new SplashKit project

First, open your code editor for instance let's say VS Code and create a new file. To do this, open your msys2 terminal, and go to the folder location, in which you want to start. **Use 'cd' command to do this** for eg. cd 'any\_folder'

Then, create the files you want to work on, use **skm new c++,** this will create the files in desired folder.

#### Step 2: Draw shapes to the screen

Next, let's draw some shapes to the screen. In the `program.cpp` file, add the following code to the `Draw` event:

```cpp

open_window("Shapes by Ekam", 800, 600);
fill_rectangle(COLOR_RED,100,400,200,300);
fill_circle(COLOR_YELLOW, 400, 300, 75);
```

This will first open a window of length 800 and width 600.

Then, This will draw a red rectangle to the screen at location (100,400) of length 200 and width 300 and a yellow circle of radius 75 with a center at (400,300).

Step 3: Respond to user input

Finally, let's make our game respond to user input. In the program.cpp file, add the following code :

#include "splashkit.h"
using namespace std;
int main()
{
    double x = 400;
    double y = 300;
    open_window("Shapes by Ekam", 800, 600);

   do{
        process_events();
        if (key_down(RIGHT_KEY))
        {
            x = x + 0.5;
        }
       clear_screen();
       fill_circle(COLOR_YELLOW, x, y, 75);
       fill_rectangle(COLOR_RED, 100, 400, 200, 300);
       refresh_screen();
    }
 while (not quit_requested());

    return 0;
}

This code will move the circle to the right when the right arrow key is pressed. We have used do-while loop, which continues to perform actions until we close the window.

In the loop, there is a procedure called process_events(), which helps in using most of the splashkit functions. Then an if statement is used, which increases the x component(center) of the circle, and then clear_screen() function is used so that it can be updated every time. And at last refresh_screen() function which refreshes our screen and we can change fps using this.

Step 4: Test your game

Now that you've added some basic gameplay mechanics, you can test your game by first compiling by using skm clang++ program.cpp . Try using the left arrow key to move the circle and see what happens! Now, you can expand this code for example, move it right, up, down and restrict this circle to move out of the window, etc.

Tips and Best Practices

  • Always try to optimize your code to make it run faster and more efficiently. This could include, using appropriate data types for instance you may not use int while dealing with pixels on the screen, but rather prefer to use double in those cases**.**

  • Try to make different functions and procedures rather than putting everything into the main, this makes the code more complex.

  • When working with SplashKit functions or rather creating projects, it can be very difficult to find bugs in the code and then debug them just by seeing them. So, I always comment on a couple of lines and run my code, using the hit-and-trial technique. So, that I get to know where the error is.

  • Always try to use pass-by-reference (&) in the parameters, as this would decrease the space taken by the code at the backend, which you might not see but it could be one of the bad traits of not good code.

    For a better understanding of other functions and procedures present in the SplashKit, you could refer to the documentation on SplashKIt Documentation

SplashKit Project

So, in the mean time, i created a Splashkit Game, it may be quite complex for the beginners to understand all of the functions, but surely one could learn a lot from it. Following is the link to the project files of the Game Project.

This Game, basically spawns a space ship, and it could move rotate in any directions, and there are powerups spawned at random instances, and some of them may be dangerous for the space ship, and some of them useful , eg, fuel, coins, xp ,etc. This game is perfect example of how the complex programs can be handled easily .

Takeaways

In this blog post, we explored how to use SplashKit to create games and animations. We looked at some of the key features and functions of SplashKit, and we provided tips and best practices for getting the most out of this powerful tool. As we have seen, SplashKit is a versatile and user-friendly toolkit that can help developers create a wide range of interactive projects. If you're interested in learning more about SplashKit, we encourage you to try out the project that we featured in this post and to share your own projects and experiences in the comments.