Thursday, September 19, 2019

Clion + SDL2 (MSYS2)

cmake_minimum_required(VERSION 3.15)
project(untitled5)

set(CMAKE_CXX_STANDARD 17)
set(EXECUTABLE_NAME "game")

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mwindows")

file(GLOB source ${CMAKE_SOURCE_DIR}/src/*.cpp)

add_executable(${EXECUTABLE_NAME} ${source})

target_link_libraries(${EXECUTABLE_NAME}
        lua
        libz
        tinyxml
        libsfml-audio
        libsfml-graphics
        libsfml-network
        libsfml-system
        libsfml-window
        mingw32
        SDL2main
        SDL2
        SDL2_image
        SDL2_ttf
        SDL2_mixer
        Qt5Core
        Qt5Gui
        Qt5Widgets)


=====================================================================
#include <SDL2/SDL.h>

SDL_Window *g_pWindow = nullptr;
SDL_Renderer *g_pRenderer = nullptr;


int main(int argc, char *argv[]) {
    // initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
        // create window
        g_pWindow = SDL_CreateWindow("Game",
                                     SDL_WINDOWPOS_CENTERED,
                                     SDL_WINDOWPOS_CENTERED,
                                     640,
                                     480,
                                     SDL_WINDOW_SHOWN);
        if (g_pWindow != nullptr) {
            // create renderer
            g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
        } else {
            // sdl not initialized
            return 1;
        }
    }
    // sdl is initialized
    // set to black
    SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
    // clear window to black
    SDL_RenderClear(g_pRenderer);
    // show window
    SDL_RenderPresent(g_pRenderer);
    // set a delay
    SDL_Delay(5000);
    //
    // clean up SDL an quit
    SDL_DestroyRenderer(g_pRenderer);
    SDL_DestroyWindow(g_pWindow);
    SDL_Quit();

    return 0;
}

Sunday, July 7, 2019

VS Code + SDL2 + SFML + Qt5

------------MSYS2-------------
//https://www.msys2.org
//update
pacman -Syu
pacman -Su

//C++
pacman -S mingw-w64-x86_64-clang
pacman -S mingw-w64-x86_64-lldb
pacman -S mingw-w64-x86_64-lld
pacman -S mingw-w64-x86_64-clang-tools-extra
pacman -S mingw-w64-x86_64-libc++

====================================== 
pacman -S mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-gdb
pacman -S mingw-w64-x86_64-boost
pacman -S mingw-w64-x86_64-ninja 

//https://www.libsdl.org
pacman -S mingw-w64-x86_64-SDL2

pacman -S mingw-w64-x86_64-SDL2_gfx
pacman -S mingw-w64-x86_64-SDL2_image 
pacman -S mingw-w64-x86_64-SDL2_mixer
pacman -S mingw-w64-x86_64-SDL2_net
pacman -S mingw-w64-x86_64-SDL2_ttf
 
//https://www.sfml-dev.org
pacman -S mingw-w64-x86_64-sfml  
 
//https://www.qt.io
pacman -S mingw-w64-x86_64-qt5
pacman -S mingw-w64-x86_64-vulkan
//https://www.gtk.org
pacman -S mingw-w64-x86_64-gtk3
pacman -S mingw-w64-x86_64-glade
pacman -S mingw-w64-x86_64-devhelp  
 

//https://www.sqlite.org
pacman -S mingw-w64-x86_64-sqlite3  
pacman -S mingw-w64-x86_64-tinyxml
pacman -S mingw-w64-x86_64-tinyxml2 
pacman -S mingw-w64-x86_64-zlib
pacman -S mingw-w64-x86_64-nlohmann_json 
pacman -S mingw-w64-x86_64-rapidjson 
 
and more:
vim 
lua
glm
git
make
-cmake 
 
---------SYSTEM_PATHES-------- 
 
PATH
C:\msys64\mingw64\bin 
 
C_INCLUDEDE_PATH
C:\msys64\mingw64\include\
 
CPLUS_INCLUDE_PATH
C:\msys64\mingw64\include\;
C:\msys64\mingw64\include\c++\9.1.0 
 
LIBRARY_PATH
C:\msys64\mingw64\lib     

==========SDL2===========
-----------a.cpp-------------

#include <SDL2\SDL.h>

int main(int argc, char *argv[])
{
    // Start SDL2
    SDL_Init(SDL_INIT_EVERYTHING);

    // Create a Window in the middle of the screen
    SDL_Window *window = 0;

    window = SDL_CreateWindow("game!",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              640, 480,
                              SDL_WINDOW_SHOWN);

    // Delay so that we can see the window appear
    SDL_Delay(2000);

    // Cleanup and Quit
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
} 


---------cli----------

g++ a.cpp -lmingw32 -mwindows -lSDL2main -lSDL2 -o a.exe
 
----------Makefile----------- 

build:
 g++ -w -std=c++14 -Wfatal-errors \
 ./src/*.cpp \
 -o game \
 -I"./include" \
 -L"./lib" \
 -lmingw32 \
 -mwindows \
 -llua \
 -lSDL2main \
 -lSDL2 \
 -lSDL2_image \
 -lSDL2_ttf \
 -lSDL2_mixer;

clean:
 rm ./game;

run:
 ./game;

--------make--------

./make
./make run
./make clean


===============VSCode================
=====================================

------------task.json----------------

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build main.cpp",
            "command": "g++",
            "args": [
                //warnings
                "-Wall",
                //debug config
                "-g",
                //cpp standard
                "-std=c++14",
                //source files
                "./src/*.cpp",
                //Directories
                "-IC:/msys64/mingw64/include; C:/msys64/mingw64/include/c++/9.1.0",
                "-LC:/msys64/mingw64/lib",
                "-lmingw32",
                "-mwindows",

                "-llua",
                //SDL2
                "-lSDL2main",
                "-lSDL2",
                "-lSDL2_image",
                "-lSDL2_ttf",
                "-lSDL2_mixer",

                //SFML
                "-lsfml-audio",
                "-lsfml-graphics",
                "-lsfml-network",
                "-lsfml-system",
                "-lsfml-window",

                //Qt5
                "-lQt5Core",
                "-lQt5Gui",
                "-lQt5Widgets",
                //output name
                "-o",
                "game.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}



---------------launch.json-----------------------

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/game.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}



-------------------c_cpp_properties.json---------------------

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/msys64/mingw64/include",
                "C:/msys64/mingw64/include/c++/9.1.0"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "C:/msys64/mingw64/include",
                    "C:/msys64/mingw64/include/c++/9.1.0"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}


--------------------settings.json-------------------------

{
    "editor.formatOnSave": true,
    "editor.suggestSelection": "first",
    "workbench.colorTheme": "One Dark Pro",
    "breadcrumbs.enabled": true,
    "debug.allowBreakpointsEverywhere": true,
    "editor.renderWhitespace": "all"
}



============Qt5======================
-------------------main.cpp---------------------------------

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGridLayout>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    //Label
    QLabel *label = new QLabel("Hello world !");
    label->setStyleSheet("QLabel:hover { color: rgb(255, 0, 0)}");
    label->setFont(QFont("Comic Sans MS", 18));
    //Button
    QPushButton *myButton = new QPushButton(QIcon("./assets/filesaveas.png"), "Push Me");
    myButton->setToolTip("Click this to turn back the hands of time");
    //Layout
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(label, 0, 0);
    layout->addWidget(myButton, 0, 1);
    //Window
    QWidget *window = new QWidget;
    window->setLayout(layout);
    window->show();

    return app.exec();
}



=============SFML==================
---------------------main.cpp-----------------------------
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Application");
    sf::CircleShape shape;
    shape.setRadius(40.f);
    shape.setPosition(100.f, 100.f);
    shape.setFillColor(sf::Color::Cyan);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

        }
         window.clear();
         window.draw(shape);
         window.display();
    }
}










MSYS2 + SDL2

1.Download and install msys2 64bit

2.Update msys2

  • Update msys2 64bit after install by running pacman -Syu if pacman needs to be updated you might have to close and reopen the terminal and run pacman -Syu again

3.Install build tools

  • pacman -S git mingw-w64-x86_64-toolchain mingw64/mingw-w64-x86_64-SDL2 mingw64/mingw-w64-x86_64-SDL2_mixer mingw64/mingw-w64-x86_64-SDL2_image mingw64/mingw-w64-x86_64-SDL2_ttf mingw64/mingw-w64-x86_64-SDL2_net mingw64/mingw-w64-x86_64-cmake make

4.Compile Hello World

hello.c

#include <stdio.h>
#include <SDL2/SDL.h>

const int WIDTH = 800, HEIGHT = 600;

int main(int argc, char *argv[]) {
  SDL_Window *window;
  SDL_Renderer *renderer;
  if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    printf("SDL_Init failed: %s\n", SDL_GetError());
    return 1;
  }

  window = SDL_CreateWindow("Hello, World!",
                                        SDL_WINDOWPOS_UNDEFINED,
                                        SDL_WINDOWPOS_UNDEFINED,
                                        WIDTH, HEIGHT,
                                        SDL_WINDOW_ALLOW_HIGHDPI);
  if(window == NULL) {
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
  }
  
  renderer = SDL_CreateRenderer(window, -1, 0);
  SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  SDL_RenderClear(renderer);

  SDL_RenderPresent(renderer);
  
  SDL_Event event;
  while(1) {
    if(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) {
        break;
      }
    }
  }

  SDL_DestroyWindow(window);

  SDL_Quit();
  return 0;
}

Compiling with mingw64

gcc -o hello -Wall hello.c `sdl2-config --cflags --libs`

Thursday, September 27, 2018

Graph (C++)


 

#pragma once
#include <string>

template<typename T>
class Vertex
{
private:
    int Index;
    T _data;

public:
    Vertex();
    Vertex(T data);
    ~Vertex();
    int IndexGet();
    void IndexSet(int index);
    T Data();
    std::string toString();
};

template<typename T>
Vertex<T>::Vertex()
{
   
}

template<typename T>
Vertex<T>::Vertex(T data)
{
    _data = data;
    Index = -1;
}

template<typename T>
Vertex<T>::~Vertex()
{
}

template<typename T>
int Vertex<T>::IndexGet()
{
    return Index;
}

template<typename T>
void Vertex<T>::IndexSet(int index)
{
    Index = index;
}

template<typename T>
T Vertex<T>::Data()
{
    return _data;
}

template<typename T>
std::string Vertex<T>::toString()
{
    return "{Vertex} " + _data;
}




=======================================
#pragma once
#include <vector>

class AdjacencyMatrix
{
private:
    int _size;
    std::vector<std::vector<float> > _matrix;
   
public:
    AdjacencyMatrix() {};
    AdjacencyMatrix(int size=0);
    ~AdjacencyMatrix();

    int Size();
    void AddDirectedEdge(int from, int to , float weight);
    void AddUndirectedEdge(int v1, int v2, float weight);
    float GetEdgeWeight(int x, int y);
    std::vector<int> GetAdjacencyList(int sourceIndex);
};


AdjacencyMatrix::AdjacencyMatrix(int size) :_size(size)
{
    std::vector< std::vector<float> > tmpVec(size, std::vector<float>(size));
    _matrix = tmpVec;
}


AdjacencyMatrix::~AdjacencyMatrix()
{

}

int AdjacencyMatrix::Size()
{
    return _size;
}

void AdjacencyMatrix::AddDirectedEdge(int from, int to, float weight)
{
    _matrix[from][to] = weight;
}

void AdjacencyMatrix::AddUndirectedEdge(int v1, int v2, float weight)
{
    _matrix[v1][v2] = weight;
    _matrix[v2][v1] = weight;
}

float AdjacencyMatrix::GetEdgeWeight(int x, int y)
{
    return _matrix[x][y];
}

std::vector<int> AdjacencyMatrix::GetAdjacencyList(int sourceIndex)
{
    std::vector<int> adjacenceList;
    for (int i = 0; i < _size; i++) {
        if (_matrix[sourceIndex][i] != 0) {
            adjacenceList.push_back(i);
        }
    }
    return adjacenceList;
}
==================================================
#pragma once

class AdjacencyMatrix;

template<typename T>
class Graph
{
private:
    std::vector<Vertex<T>*> _vertices;
    AdjacencyMatrix* _adjacencyMatrix;

public:
    Graph() {};
    Graph(std::vector<Vertex<T>*> vertices);
    ~Graph();

    std::vector<Vertex<T>*> Vertices();
    void CreateDirectedEdge(int fromIndex, int toIndex, float weight);
    void CreateDirectedEdge(Vertex<T> from, Vertex<T> to, float weight);
    void CreateUndirectedEdge(int v1, int v2, float weight);
    void CreateUndirectedEdge(Vertex<T> v1, Vertex<T> v2, float weight);
    std::vector<Vertex<T>*> GetAdjacentVertices(int sourceIndex);
    std::vector<Vertex<T>*> GetAdjacentVertices(Vertex<T> source);
    float GetEdgeWeight(Vertex<T> v1, Vertex<T> v2);
    std::string toString();
};


#include <vector>
#include <string>
#include "AdjacencyMatrix.h"
#include "Vertex.h"
//#include "Graph.h"



template<typename T>
Graph<T>::Graph(std::vector<Vertex<T>*> vertices)
{
    _vertices = vertices;
    for (int i = 0; i < _vertices.size(); i++) {
        _vertices.at(i)->IndexSet(i);
    }
    _adjacencyMatrix = new AdjacencyMatrix(_vertices.size());
}

template<typename T>
Graph<T>::~Graph()
{
    delete _adjacencyMatrix;
}

template<typename T>
std::vector<Vertex<T>*> Graph<T>::Vertices()
{
    return _vertices;
}

template<typename T>
void Graph<T>::CreateDirectedEdge(int fromIndex, int toIndex, float weight) {
    if (weight == 0) {
        weight = 1;
    }
    _adjacencyMatrix->AddDirectedEdge(fromIndex, toIndex, weight);
}

template<typename T>
void Graph<T>::CreateDirectedEdge(Vertex<T> from, Vertex<T> to, float weight) {
    if (weight == 0) {
        weight = 1;
    }
    this->CreateDirectedEdge(from.IndexGet(), to.IndexGet(), weight);
}

template<typename T>
void Graph<T>::CreateUndirectedEdge(int v1, int v2, float weight) {
    if (weight == 0) {
        weight = 1;
    }
    _adjacencyMatrix->AddUndirectedEdge(v1, v2, weight);
}

template<typename T>
void Graph<T>::CreateUndirectedEdge(Vertex<T> v1, Vertex<T> v2, float weight) {
    if (weight == 0) {
        weight = 1;
    }
    this->CreateUndirectedEdge(v1.IndexGet(), v2.IndexGet(), weight);
}

template<typename T>
std::vector<Vertex<T>*> Graph<T>::GetAdjacentVertices(int sourceIndex) {
    std::vector<int> adjacentIndices = _adjacencyMatrix->GetAdjacencyList(sourceIndex);
    std::vector<Vertex<T>*> adjacentVertices;

    for (int vertexIndex : adjacentIndices) {
        adjacentVertices.push_back(_vertices.at(vertexIndex));
    }

    return adjacentVertices;
}

template<typename T>
std::vector<Vertex<T>*> Graph<T>::GetAdjacentVertices(Vertex<T> source) {
    return GetAdjacentVertices(source.IndexGet());
}

template<typename T>
float Graph<T>::GetEdgeWeight(Vertex<T> v1, Vertex<T> v2) {
    return _adjacencyMatrix->GetEdgeWeight(v1.IndexGet(), v2.IndexGet());
}

template<typename T>
std::string Graph<T>::toString() {

    std::string sb;

    sb.append("Graph: \n");

    for (Vertex<T>* vertex : _vertices) {
        sb.append(vertex->Data());
        sb.append("\t");
        std::vector<Vertex<T>*> adjacentVertices = GetAdjacentVertices(*vertex);
        if (adjacentVertices.size() > 0) {
            sb.append("Edge to: ");
            for (Vertex<T>* adjVertex : adjacentVertices) {
                sb.append(adjVertex->Data());
                sb.append("(w=");

                sb.append(std::to_string(GetEdgeWeight(*vertex, *adjVertex)));
                sb.append(") ");
            }
        }
        else {
            sb.append("No outgoing edges");
        }
        sb.append("\n");
    }

    return sb;
}
=============================================

#include <iostream>
#include <vector>
#include <string>
#include "Vertex.h"
#include "Graph.h"

int main()
{
    std::cout << "Graphs" << std::endl;
   
    //auto* v1 = new Vertex<std::string>("v1");
    //std::string xxx = v1->toString();
    //std::cout << xxx << std::endl;

    Vertex<std::string>* v1 = new Vertex<std::string>("v1");
    Vertex<std::string>* v2 = new Vertex<std::string>("v2");
    Vertex<std::string>* v3 = new Vertex<std::string>("v3");
    Vertex<std::string>* v4 = new Vertex<std::string>("v4");
   
    std::vector<Vertex<std::string>*> vertices;

    vertices.push_back(v1);
    vertices.push_back(v2);
    vertices.push_back(v3);
    vertices.push_back(v4);

    Graph<std::string>* graph = new Graph<std::string>(vertices);

    graph->CreateDirectedEdge(*v1, *v2, 3);
    graph->CreateDirectedEdge(*v4, *v1, 1);
    graph->CreateDirectedEdge(*v2, *v3, 1);
    graph->CreateDirectedEdge(*v2, *v4, -5);

    std::cout << graph->toString() << std::endl;

    std::cout << "End " << std::endl;
   
    delete graph;
   
    return 0;
}

Graph:
v1 Edge to: v2(w=3.0)
v2 Edge to: v3(w=1.0) v4(w=-2.0)
v3 No edges
v4 Edge to: v1(w=1.0)