Add misc and sfml modules with initial implementations, including new classes, assets, and CMake integration.
@@ -1 +1 @@
|
|||||||
containers
|
CMakeSFMLProject
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<component name="CMakePythonSetting">
|
<component name="CMakePythonSetting">
|
||||||
<option name="pythonIntegrationState" value="YES" />
|
<option name="pythonIntegrationState" value="YES" />
|
||||||
</component>
|
</component>
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/cpp/containers">
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/cpp/sfml">
|
||||||
<contentRoot DIR="$PROJECT_DIR$" />
|
<contentRoot DIR="$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -5,3 +5,4 @@ add_subdirectory(intro)
|
|||||||
add_subdirectory(pointer)
|
add_subdirectory(pointer)
|
||||||
add_subdirectory(classes)
|
add_subdirectory(classes)
|
||||||
add_subdirectory(containers)
|
add_subdirectory(containers)
|
||||||
|
add_subdirectory(misc)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 4.1)
|
cmake_minimum_required(VERSION 4.1)
|
||||||
project(containers)
|
project(containers)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_FLAGS -pedantic-errors)
|
set(CMAKE_CXX_FLAGS -pedantic-errors)
|
||||||
|
|
||||||
add_executable(containers main.cpp
|
add_executable(containers main.cpp
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ int main() {
|
|||||||
printArrayAtPosition(numbers, 19);
|
printArrayAtPosition(numbers, 19);
|
||||||
|
|
||||||
// vector
|
// vector
|
||||||
std::vector<int> v;
|
std::vector<int> v = {1, 2, 3, 4, 5};
|
||||||
v.push_back(10);
|
v.push_back(10);
|
||||||
v.push_back(20);
|
v.push_back(20);
|
||||||
v.push_back(30);
|
v.push_back(30);
|
||||||
@@ -54,12 +54,27 @@ int main() {
|
|||||||
printArrayAtPosition(v.data(), 1);
|
printArrayAtPosition(v.data(), 1);
|
||||||
|
|
||||||
// list
|
// list
|
||||||
std::list<int> l;
|
std::list<int> l = {1, 2, 3, 4, 5};
|
||||||
|
l.push_front(0);
|
||||||
std::list<int>::iterator it;
|
std::list<int>::iterator it;
|
||||||
|
|
||||||
for (it = l.begin(); it != l.end(); ++it) {
|
for (it = l.begin(); it != l.end(); ++it) {
|
||||||
std::cout << *it << std::endl;
|
std::cout << *it << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--it; --it;
|
||||||
|
l.insert(it, 100);
|
||||||
|
|
||||||
|
for (it = l.begin(); it != l.end(); ++it) {
|
||||||
|
std::cout << *it << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
--it; --it;
|
||||||
|
l.erase(it);
|
||||||
|
|
||||||
|
for (it = l.begin(); it != l.end(); ++it) {
|
||||||
|
std::cout << *it << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 4.1)
|
||||||
|
project(misc)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
set(CMAKE_CXX_FLAGS -pedantic-errors)
|
||||||
|
|
||||||
|
add_executable(misc main.cpp
|
||||||
|
inlines.cpp)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "inlines.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void Value::Print() const {
|
||||||
|
std::cout << "Value: " << value << std::endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Created by David Hackbarth on 27.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MISC_INLINES_H
|
||||||
|
#define MISC_INLINES_H
|
||||||
|
|
||||||
|
class Value {
|
||||||
|
public:
|
||||||
|
Value(int val) : value(val) {} // implicit inline
|
||||||
|
int getValue() const { return value; } // implicit inline
|
||||||
|
|
||||||
|
// explicit inline, but redundant because it is already implicit inline
|
||||||
|
inline void setValue(int val) { value = val; }
|
||||||
|
|
||||||
|
inline void Print() const; // explicit inline
|
||||||
|
|
||||||
|
private:
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //MISC_INLINES_H
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
|
#include <random>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#include "inlines.h"
|
||||||
|
#include "structs_enums_unions.h"
|
||||||
|
#include "templates.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int* pI = new int(0);
|
||||||
|
long* pL = new long(0);
|
||||||
|
|
||||||
|
safeDelete<int>(pI);
|
||||||
|
safeDelete(pL);
|
||||||
|
|
||||||
|
Node<int> n1(10);
|
||||||
|
Node<int> n2(20);
|
||||||
|
Node<int> n3(30);
|
||||||
|
|
||||||
|
n1.next = &n2;
|
||||||
|
n2.next = &n3;
|
||||||
|
|
||||||
|
// structs
|
||||||
|
Vector3 v1 = { 1.0f, 2.0f, 3.0f };
|
||||||
|
Vector3 v2 = { 4.0f, 5.0f, 6.0f };
|
||||||
|
bool isEqual = v1 == v2;
|
||||||
|
v1 = v1 * 3.0f;
|
||||||
|
v1 *= 3.0f;
|
||||||
|
Vector3 v3 = 3.0f * v2;
|
||||||
|
std::cout << "v1: " << v1 << std::endl;
|
||||||
|
|
||||||
|
// enums
|
||||||
|
Weekdays day = Weekdays::Friday;
|
||||||
|
std::cout << "day: " << day << std::endl;
|
||||||
|
|
||||||
|
// union
|
||||||
|
ScreenSize screen_size;
|
||||||
|
screen_size.u.width = 1920;
|
||||||
|
screen_size.u.height = 1080;
|
||||||
|
// screen_size.width = 1920;
|
||||||
|
// screen_size.height = 1080;
|
||||||
|
std::cout << sizeof(screen_size) << std::endl;
|
||||||
|
|
||||||
|
// inline
|
||||||
|
Value v(42);
|
||||||
|
v.setValue(69);
|
||||||
|
std::cout << "Value: " << v.getValue() << std::endl;
|
||||||
|
|
||||||
|
// random
|
||||||
|
// rand(); // old c implementation, don't use it
|
||||||
|
// srand(time(NULL)); // old c implementation, don't use it
|
||||||
|
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
|
||||||
|
std::uniform_int_distribution<int> distribution(0,100);
|
||||||
|
|
||||||
|
std::cout << distribution(generator) << std::endl;
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
std::cout << distribution(generator) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
//
|
||||||
|
// Created by David Hackbarth on 27.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MISC_STRUCTS_ENUMS_UNIONS_H
|
||||||
|
#define MISC_STRUCTS_ENUMS_UNIONS_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
struct Vector3 {
|
||||||
|
// public
|
||||||
|
float x, y, z;
|
||||||
|
|
||||||
|
bool operator==(const Vector3 &other) const {
|
||||||
|
return x == other.x && y == other.y && z == other.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const Vector3 &other) const { return !(*this == other); }
|
||||||
|
bool operator<(const Vector3 &other) const { return x < other.x && y < other.y && z < other.z; }
|
||||||
|
bool operator>(const Vector3 &other) const { return other < *this; }
|
||||||
|
bool operator<=(const Vector3 &other) const { return !(*this > other); }
|
||||||
|
bool operator>=(const Vector3 &other) const { return !(*this < other); }
|
||||||
|
|
||||||
|
Vector3 operator*(float scalar) const { return {x * scalar, y * scalar, z * scalar}; }
|
||||||
|
|
||||||
|
void operator*=(float scalar) {
|
||||||
|
x *= scalar;
|
||||||
|
y *= scalar;
|
||||||
|
z *= scalar;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// private
|
||||||
|
};
|
||||||
|
|
||||||
|
inline Vector3 operator*(float scalar, const Vector3 &v) { return v * scalar; }
|
||||||
|
|
||||||
|
inline std::ostream &operator<<(std::ostream &os, const Vector3 &v) {
|
||||||
|
os << "(" << v.x << "/" << v.y << "/" << v.z << ")";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Weekdays {
|
||||||
|
Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday,
|
||||||
|
|
||||||
|
FORCE_DWORD = 0x7fffffff // DWORD -> double word -> unsigned 32bit int, word -> unsigned 16bit int
|
||||||
|
};
|
||||||
|
|
||||||
|
union ScreenSize {
|
||||||
|
// struct {
|
||||||
|
// unsigned short width; // first 16bit
|
||||||
|
// unsigned short height; // second 16bit
|
||||||
|
// };
|
||||||
|
|
||||||
|
struct {
|
||||||
|
unsigned short width; // first 16bit
|
||||||
|
unsigned short height; // second 16bit
|
||||||
|
} u;
|
||||||
|
|
||||||
|
unsigned int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //MISC_STRUCTS_ENUMS_UNIONS_H
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by David Hackbarth on 27.02.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MISC_TEMPLATES_H
|
||||||
|
#define MISC_TEMPLATES_H
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void safeDelete(T*& pObj)
|
||||||
|
{
|
||||||
|
if (pObj != nullptr)
|
||||||
|
{
|
||||||
|
delete pObj;
|
||||||
|
pObj = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
T data;
|
||||||
|
Node* next;
|
||||||
|
|
||||||
|
Node(T data) : data(data), next(nullptr) {}
|
||||||
|
};
|
||||||
|
#endif //MISC_TEMPLATES_H
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(CMakeSFMLProject LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(SFML
|
||||||
|
GIT_REPOSITORY https://github.com/SFML/SFML.git
|
||||||
|
GIT_TAG 3.0.2
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
EXCLUDE_FROM_ALL
|
||||||
|
SYSTEM)
|
||||||
|
FetchContent_MakeAvailable(SFML)
|
||||||
|
|
||||||
|
add_executable(main src/main.cpp
|
||||||
|
src/elements/Paddle.cpp
|
||||||
|
src/elements/Paddle.h
|
||||||
|
src/elements/Ball.cpp
|
||||||
|
src/elements/Ball.h)
|
||||||
|
target_compile_features(main PRIVATE cxx_std_17)
|
||||||
|
target_link_libraries(main PRIVATE SFML::Graphics)
|
||||||
|
|
||||||
|
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT main)
|
||||||
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 2.7 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 119 B |
|
After Width: | Height: | Size: 119 B |
|
After Width: | Height: | Size: 119 B |
|
After Width: | Height: | Size: 119 B |
|
After Width: | Height: | Size: 119 B |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 3.6 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 4.0 KiB |
|
After Width: | Height: | Size: 142 B |
|
After Width: | Height: | Size: 142 B |
|
After Width: | Height: | Size: 142 B |
|
After Width: | Height: | Size: 142 B |
|
After Width: | Height: | Size: 142 B |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 5.7 KiB |
|
After Width: | Height: | Size: 5.7 KiB |
|
After Width: | Height: | Size: 5.6 KiB |
|
After Width: | Height: | Size: 6.1 KiB |
|
After Width: | Height: | Size: 6.3 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 6.1 KiB |
|
After Width: | Height: | Size: 5.8 KiB |
|
After Width: | Height: | Size: 6.0 KiB |
|
After Width: | Height: | Size: 5.6 KiB |
|
After Width: | Height: | Size: 5.6 KiB |
|
After Width: | Height: | Size: 5.6 KiB |
|
After Width: | Height: | Size: 6.1 KiB |
|
After Width: | Height: | Size: 6.2 KiB |
|
After Width: | Height: | Size: 6.0 KiB |