Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Sfml Quick Projects

WIP GAMES, WIP FOREVER! Learning C++
I have been trying to focus and make something complete, Other projects I made fell into obscurity.
I have never taken classes on programming so my code is probable weird, Made more artwork then code
so most of my projects are testing how to get things working using some lib or another.

NEWER SFML PROJECT :: TICTAKTOE INTERFACE.
I was getting the hang of making things in sfml, decided to make a small part of a game as fast as possible.
This project just has a start button that then displays the board and when you click it it changes to the opposing
tile type on next click. This project took almost no time to make since I had to only make the tiles using a existing
blank tile and then a quick and dirty hashing out the game mechanics. This does not even have a winning case
it's just a board that displays one tile type then the other. This was one of a few under 1 hour dev projects I was
making a few months back.
tick1.png
tick2.png

Code:
 

#include <SFML/Window.hpp>

#include <SFML/Graphics.hpp>

#include <iostream>

 

int FlatMenu(int Recsize1,int Recsize2,int Recposx,int Recposy,sf::Font Font,sf::Texture BackGround,sf::Sprite SpriteNode,sf::RenderWindow* Pwindow,int ImgSizex,int ImgSizey)

{

    //THE START MENU BOX

    sf::RectangleShape Rectangle(sf::Vector2f(Recsize1, Recsize2));

    Rectangle.setFillColor(sf::Color(200, 200, 200));

    Rectangle.setOutlineThickness(1);

    Rectangle.setOutlineColor(sf::Color(100, 100, 100));

    Rectangle.setPosition(Recposx,Recposy);

    //SETUP A TEXTURE TO PUT ONTO THE SHAPE

    Rectangle.setTexture(&BackGround);

    Rectangle.setTextureRect(sf::IntRect(1, 1, ImgSizex, ImgSizey));

    SpriteNode.setTexture(BackGround);

    Pwindow->draw(Rectangle);

    return 1;

}

 

int main()

{

    //CREATE THE WINDOW

    sf::RenderWindow Window(sf::VideoMode(800, 600), "My window");

    sf::RenderWindow* Pwindow;

    Window.setVerticalSyncEnabled(true);

    Window.setFramerateLimit(60);

    //SETUP A FONT

    sf::Sprite SpriteNode;

    sf::Texture BackGround;

    sf::Texture TileX;

    sf::Texture TileO;

    sf::Texture Board;

    sf::Font Font;

 

    //TRIGGERS NEED TO PROPAGATE THREW MORE THEN ONE FRAME SO THEY ARE MADE HERE

    int TurnTrigger=1;

 

    int TriggerArray[9] = {3,3,3,3,3,3,3,3,3};

    int *Trigger=TriggerArray;

 

    if (!Font.loadFromFile("arial.ttf"))

    {

        //Error...

    }

    //LOAD A IMAGE

    if (!BackGround.loadFromFile("../../Media/WindowBG.png"))

    {

        //Error...

    }

    if (!TileX.loadFromFile("../../Media/TileX.png"))

    {

        //Error...

    }

    if (!TileO.loadFromFile("../../Media/TileO.png"))

    {

        //Error...

    }

    if (!Board.loadFromFile("../../Media/Board.png"))

    {

        //Error...

    }

    //CREATE THE SCENE VARIABLE

    int Scene=1;

    // WHILE THE WINDOW IS OPEN RUN THE FOLLOWING CODE

    while (Window.isOpen())

    {

        // CHECK ALL THE EVENTS TRIGGERED SINCE THE LAST ITTERATION OF THIS LOOP

        sf::Event event;

        while (Window.pollEvent(event))

        {

            //CLEAR LAST SCENE

            Window.clear(sf::Color::Black);

            //USE THE SCENE VAR TO INDICATE WHAT SCENE TO DISPLAY

            if (Scene==1)

            {

                sf::Text Text;

                Text.setFont(Font); // font is a sf::Font

                Text.setString("TickTacToe Board Using GUI Elements");

                Text.setCharacterSize(24); // in pixels, not points!

                Window.draw(Text);

                //RUN THE FLATMENU FUNCTION

                int SizeX=226;

                int SizeY=174;

                int PosX=275;

                int PosY=200;

                Pwindow=&Window;

                FlatMenu(SizeX,SizeY,PosX,PosY,Font,BackGround,SpriteNode,Pwindow,SizeX,SizeY);

                //ON LEFT MOUSE CLICK

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))

                {

                    //GET MOUSE POSITION

                    sf::Vector2i localPosition = sf::Mouse::getPosition(Window);

                    //IF THE MOUSE IS WITHIN THE BOX BEING DISPLAYED

                    if (localPosition.x>PosX&&localPosition.x<PosX+SizeX)

                    {

                        if (localPosition.y>PosY&&localPosition.y<PosY+SizeY)

                        {

                            //KILL THE BOX TO SHOW IT WORKS

                            Scene++;

                        }

                    }

                }

            }

            //SECOND SCENE IS THE GAMEBOARD

            else if (Scene==2)

            {

                //RUN THE FLATMENU FUNCTION PLACING EACH TILE OF THE BOARD

                int PosXOffset=50;

                int PosYOffset=50;

                int PosX=PosXOffset;

                int PosY=PosYOffset;

                int SizeX=138;

                int SizeY=138;

                //GET MOUSE POSITION

                sf::Vector2i localPosition = sf::Mouse::getPosition(Window);

                //RUN A FOR X FOR Y LOOP

                for (int i=0;i<3;i++)

                {

                    for (int h=0;h<3;h++)

                    {

                        Pwindow=&Window;

                        //USE AS BINDINGS FOR THE CORDS OF TILES

                        if (TriggerArray[i*3+h]==1){FlatMenu(SizeX,SizeY,PosX+(SizeX*h),PosY+(SizeY*i),Font,TileO,SpriteNode,Pwindow,SizeX,SizeY);}

                        else if (TriggerArray[i*3+h]==2){FlatMenu(SizeX,SizeY,PosX+(SizeX*h),PosY+(SizeY*i),Font,TileX,SpriteNode,Pwindow,SizeX,SizeY);}

                        else {FlatMenu(SizeX,SizeY,PosX+(SizeX*h),PosY+(SizeY*i),Font,Board,SpriteNode,Pwindow,SizeX,SizeY);}

                        //ON LEFT MOUSE CLICK CHANGE THE TILE TO THE TYPE FOR THE CURRENT PLAYER

                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))

                        {

                            //IF THE MOUSE X IS > THE (CURRENT TILE'S X)+OFFSETX AND THE MOUSE X IS < THE (CURRENT TILE'S X)+138

                            if (localPosition.x>(h*138)+PosXOffset && localPosition.x<(h*138)+PosXOffset+SizeX)

                            {

                                if (localPosition.y>(i*138)+PosYOffset && localPosition.y<(i*138)+PosYOffset+SizeY)

                                {

                                    if (TurnTrigger==1)

                                    {

                                        //I*H=Y +h=X TO GET POSITION IN TRIGGER ARRAY THATS A 1D REPERSENTATION OF THE SAME BOARD

                                        TriggerArray[i*3+h]=2;

                                        TurnTrigger=2;

                                    }

                                    else if (TurnTrigger==2)

                                    {

                                        TriggerArray[i*3+h]=1;

                                        TurnTrigger=1;

                                    }

                                }

                            }

                        }

                    }

                }

            }

            //IF A WINDOW CLOSE EVENT FIRES THEN KILL THE WINDOW

            if (event.type == sf::Event::Closed)

            {

                Window.close();

            }

            //DISPLAY THIS FRAME

            Window.display();

        }

    }

 

    return 0;

}

 

Getting into the older stuff!
Here is another SFML project that does next to nothing, This one moves little guys around the screen changing the direction the object
moves depending on what part of the image your hovering over with the mouse. I ended up spending more time drawing the funny little
character then I did coding it. This one also has the issue that the mouse hover event only fires 1 time and does not recheck unless you
move the mouse again so you have to keep the mouse moving all the time to keep the little guy moving. I could fix it but I was just doing
a little practice and was trying to restrict my self to taking less then 1 hour in total to make it. And looking at the code for a second
I did not even bother to remove all of the tic tak toe code that I used as a base.
move1.png
move2.png

Code:
 

#include <SFML/Window.hpp>

#include <SFML/Graphics.hpp>

#include <iostream>

 

sf::RectangleShape FlatMenu(int Recsize1,int Recsize2,int Recposx,int Recposy,sf::Font Font,sf::Texture BackGround,sf::Sprite SpriteNode,sf::RenderWindow* Pwindow,int ImgSizex,int ImgSizey)

{

    //THE START MENU BOX

    sf::RectangleShape Rectangle(sf::Vector2f(Recsize1, Recsize2));

    Rectangle.setFillColor(sf::Color(200, 200, 200));

    Rectangle.setOutlineThickness(0);

    Rectangle.setOutlineColor(sf::Color(100, 100, 100));

    Rectangle.setPosition(Recposx,Recposy);

    //SETUP A TEXTURE TO PUT ONTO THE SHAPE

    Rectangle.setTexture(&BackGround);

    Rectangle.setTextureRect(sf::IntRect(1, 1, ImgSizex, ImgSizey));

    SpriteNode.setTexture(BackGround);

    return Rectangle;

}

 

int main()

{

    //CREATE THE WINDOW

    sf::RenderWindow Window(sf::VideoMode(800, 600), "My window");

    sf::RenderWindow* Pwindow;

    Window.setVerticalSyncEnabled(true);

    Window.setFramerateLimit(60);

    //SETUP A FONT

    sf::Sprite SpriteNode;

    sf::Sprite SpriteNode2;

    sf::Texture BackGround;

    sf::Texture TileX;

    sf::Texture TileO;

    sf::Texture Board;

    sf::Texture Character;

    sf::Font Font;

 

    int ShapeX=100;

    int ShapeY=100;

    int ShapeX2=100;

    int ShapeY2=100;

    int S=100;

 

    //TRIGGERS NEED TO PROPAGATE THREW MORE THEN ONE FRAME SO THEY ARE MADE HERE

    int TriggerArray[9] = {3,3,3,3,3,3,3,3,3};

    int *Trigger=TriggerArray;

    //LOAD RECOURCE FILES

    if (!Font.loadFromFile("arial.ttf"))

    {

        //Error...

    }

    //LOAD A IMAGE

    if (!BackGround.loadFromFile("../../Media/WindowBG.png"))

    {

        //Error...

    }

    if (!TileX.loadFromFile("../../Media/TileX.png"))

    {

        //Error...

    }

    if (!TileO.loadFromFile("../../Media/TileO.png"))

    {

        //Error...

    }

    if (!Board.loadFromFile("../../Media/Board.png"))

    {

        //Error...

    }

    if (!Character.loadFromFile("../../Media/Character.png"))

    {

        //Error...

    }

    //CREATE THE SCENE VARIABLE

    int Scene=1;

    // WHILE THE WINDOW IS OPEN RUN THE FOLLOWING CODE

    while (Window.isOpen())

    {

        // CHECK ALL THE EVENTS TRIGGERED SINCE THE LAST ITTERATION OF THIS LOOP

        sf::Event event;

        while (Window.pollEvent(event))

        {

            //CLEAR LAST SCENE

            Window.clear(sf::Color::Black);

            //USE THE SCENE VAR TO INDICATE WHAT SCENE TO DISPLAY

            if (Scene==1)

            {

                sf::Text Text;

                Text.setFont(Font); // font is a sf::Font

                Text.setString("Hover Movement on GUI Elements");

                Text.setCharacterSize(24); // in pixels, not points!

                Window.draw(Text);

                //RUN THE FLATMENU FUNCTION

                int SizeX=226;

                int SizeY=174;

                int PosX=275;

                int PosY=200;

                Window.draw(FlatMenu(SizeX,SizeY,PosX,PosY,Font,BackGround,SpriteNode,Pwindow,SizeX,SizeY));

                //ON LEFT MOUSE CLICK

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))

                {

                    //GET MOUSE POSITION

                    sf::Vector2i localPosition = sf::Mouse::getPosition(Window);

                    //IF THE MOUSE IS WITHIN THE BOX BEING DISPLAYED

                    if (localPosition.x>PosX&&localPosition.x<PosX+SizeX)

                    {

                        if (localPosition.y>PosY&&localPosition.y<PosY+SizeY)

                        {

                            //KILL THE BOX TO SHOW IT WORKS

                            Scene++;

                        }

                    }

                }

            }

            //SECOND SCENE IS THE GAMEBOARD

            else if (Scene==2)

            {

                int SizeX=100;

                int SizeY=100;

                ShapeX2++;

                ShapeY2++;

                Window.draw(FlatMenu(SizeX,SizeY,ShapeX2,ShapeY2,Font,Character,SpriteNode2,Pwindow,SizeX,SizeY));

                Window.draw(FlatMenu(SizeX,SizeY,ShapeX,ShapeY,Font,Character,SpriteNode,Pwindow,SizeX,SizeY));

                //GET MOUSE POSITION

                sf::Vector2i localPosition = sf::Mouse::getPosition(Window);

                //IF IN THE TOP RIGHT CORNER

                if (localPosition.x>ShapeX+(SizeX/2)&&localPosition.x<ShapeX+SizeX)

                {

                    if (localPosition.y>ShapeY+(SizeY/2)&&localPosition.y<ShapeY+SizeY)

                    {

                        ShapeX++;

                        ShapeY++;

                    }

                }

                //IF IN THE BOTTOM LEFT

                if (localPosition.x>ShapeX&&localPosition.x<ShapeX+(SizeX/2))

                {

                    if (localPosition.y>ShapeY&&localPosition.y<ShapeY+(SizeY/2))

                    {

                        ShapeX--;

                        ShapeY--;

                    }

                }

                //IF IN THE TOP LEFT

                if (localPosition.x>ShapeX&&localPosition.x<ShapeX+(SizeX/2))

                {

                    if (localPosition.y>ShapeY+(SizeY/2)&&localPosition.y<ShapeY+ShapeY)

                    {

                        ShapeX--;

                        ShapeY++;

                    }

                }

                //IF IN THE BOTTOM RIGHT

                if (localPosition.x>ShapeX+(SizeX/2)&&localPosition.x<ShapeX+SizeX)

                {

                    if (localPosition.y>ShapeY&&localPosition.y<ShapeY+(SizeY/2))

                    {

                        ShapeX++;

                        ShapeY--;

                    }

                }

            }

            //IF A WINDOW CLOSE EVENT FIRES THEN KILL THE WINDOW

            if (event.type == sf::Event::Closed)

            {

                Window.close();

            }

            //DISPLAY THIS FRAME

            Window.display();

        }

    }

 

    return 0;

}

 

Super early project.
This next one is a full on 2d game engine that I started trying to make and was also one of the first things I ever
coded, I was shooting too high. I set this one up so that you could open different windows that would run splash screens on a timer
and the landscape is a manual sinusoidal wave generator that populates a tile map system that can be expanded to any size and has
any possible number of waves able to be generated over the first wave so you could have the landscape be not too wave like.
It also has a sound system setup so music plays in the background. I also started getting into doing collision detection for the moving
objects but realized that I needed more self training on how physics engines work and how to do aabb collision ect.
This one has too much code to post here, and It's super messy crap code since I had no idea what I was doing for the most part.
Game_Engine1.png
Game_Engine2.png

Game_Engine3.png
Game_Engine4.png


I have more projects to throw on here but this is a good start to the topic, I will update in a bit.
 
MORE ON SFML PROJECTS!
I had to go into the depth of my file system and find the old projects backup folder to extract more of this project.
This time It's the same engine but into more recent history by a tiny bit. At this point I had managed to make a
function that would skin the edges of the boundary between tile types using one of two slop tiles for the type of edge.
I also made a animated character that is bound to the center of the screen and just runs in place. And for the kicker I
made the random walk AI so it was running on a timer to smooth out the random walking from shaking to making actual
random walk movements. And at this point I had put collision detection for the random moving objects. But this was right
at the end of my work on the project so I never fixed the issue with the monsters phasing threw the ground some times as
you can see in the image.
Game1.png
Game2.png


GETTING INTO PHYSICS!!
After that first project I noticed how little I knew about what people are doing to get the physics effects I see in all the
games all over the place. So I got another over reaching idea in mind to create a physics engine from scratch using
my previous projects resource loading and window management code as a base since I had made it so that everything
in the game was dynamically configured from text file so that the player could just add a new type of monster and a image
in text files and the game would then have the ability to generate it. So I jumped into doing physics. Now that I look back
It's nice to know what I learned from the project but people should just use existing physics libs because It's a giant undertaking.
The video below shows the engine in action, I had made all the basics of friction mass with a impulse force system for applying
force to the objects to make them move for player movement. You will also notice that I made it so that every X time all the
objects in the scene have a force applied to them, just keeping the demo interesting. I was proud of this one since I wrote
everything down to the base myself and only used sfml for easy graphics display.
VIDEO OF PHYSICS ENGINE IN ACTION!!<------------------------------------------------------------------------------------
http://sendvid.com/vereogs7
SCREENSHOTS OF PHYSICS!!
falling1.png
falling2.png


I will have to go back and clean up my code a bit then post the source so people can mess with it.
BTW The weird numbers in the corner are just from a messed up frame rate display i never bothered to fix.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top