Compare commits
10 commits
7565529412
...
841d08ef95
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
841d08ef95 | ||
|
|
461002939d | ||
|
|
14677d87fd | ||
|
|
d2cfedf993 | ||
|
|
561fdbbb6b | ||
|
|
54bc6b9415 | ||
|
|
438d633a78 | ||
|
|
e880ded96f | ||
|
|
e0e942e809 | ||
|
|
ce38d9f561 |
287
.clang-format
Normal file
287
.clang-format
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
---
|
||||
Language: Cpp
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignArrayOfStructures: None
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: true
|
||||
AlignConsecutiveBitFields:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveDeclarations:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: true
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveMacros:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveShortCaseStatements:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCaseArrows: false
|
||||
AlignCaseColons: false
|
||||
AlignConsecutiveTableGenBreakingDAGArgColons:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveTableGenCondOperatorColons:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveTableGenDefinitionColons:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
AlignFunctionDeclarations: false
|
||||
AlignFunctionPointers: false
|
||||
PadOperators: false
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: Align
|
||||
AlignTrailingComments:
|
||||
Kind: Always
|
||||
OverEmptyLines: 0
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowBreakBeforeNoexceptSpecifier: Never
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseExpressionOnASingleLine: true
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortCompoundRequirementOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortNamespacesOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AttributeMacros:
|
||||
- __capability
|
||||
BinPackArguments: true
|
||||
BinPackParameters: BinPack
|
||||
BitFieldColonSpacing: Both
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: Never
|
||||
AfterEnum: false
|
||||
AfterExternBlock: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakAdjacentStringLiterals: true
|
||||
BreakAfterAttributes: Leave
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakAfterReturnType: None
|
||||
BreakArrays: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeConceptDeclarations: Always
|
||||
BreakBeforeBraces: Attach
|
||||
BreakBeforeInlineASMColon: OnlyMultiline
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakBinaryOperations: Never
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
BreakFunctionDefinitionParameters: false
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakStringLiterals: true
|
||||
BreakTemplateDeclarations: MultiLine
|
||||
ColumnLimit: 0
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
EmptyLineAfterAccessModifier: Never
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IfMacros:
|
||||
- KJ_IF_MAYBE
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories:
|
||||
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '.*'
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
IncludeIsMainRegex: '(Test)?$'
|
||||
IncludeIsMainSourceRegex: ''
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseBlocks: false
|
||||
IndentCaseLabels: false
|
||||
IndentExportBlock: true
|
||||
IndentExternBlock: AfterExternBlock
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentRequiresClause: true
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
InsertBraces: false
|
||||
InsertNewlineAtEOF: false
|
||||
InsertTrailingCommas: None
|
||||
IntegerLiteralSeparator:
|
||||
Binary: 0
|
||||
BinaryMinDigits: 0
|
||||
Decimal: 0
|
||||
DecimalMinDigits: 0
|
||||
Hex: 0
|
||||
HexMinDigits: 0
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLines:
|
||||
AtEndOfFile: false
|
||||
AtStartOfBlock: true
|
||||
AtStartOfFile: true
|
||||
KeepFormFeed: false
|
||||
LambdaBodyIndentation: Signature
|
||||
LineEnding: DeriveLF
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MainIncludeChar: Quote
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Auto
|
||||
ObjCBlockIndentWidth: 4
|
||||
ObjCBreakBeforeNestedBlockParam: true
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PackConstructorInitializers: BinPack
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakBeforeMemberAccess: 150
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakOpenParenthesis: 0
|
||||
PenaltyBreakScopeResolution: 500
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyIndentedWhitespace: 0
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PointerAlignment: Right
|
||||
PPIndentWidth: -1
|
||||
QualifierAlignment: Leave
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: Always
|
||||
RemoveBracesLLVM: false
|
||||
RemoveEmptyLinesInUnwrappedLines: false
|
||||
RemoveParentheses: Leave
|
||||
RemoveSemicolon: false
|
||||
RequiresClausePosition: OwnLine
|
||||
RequiresExpressionIndentation: OuterScope
|
||||
SeparateDefinitionBlocks: Leave
|
||||
ShortNamespaceLines: 1
|
||||
SkipMacroDefinitionBody: false
|
||||
SortIncludes: CaseSensitive
|
||||
SortJavaStaticImport: Before
|
||||
SortUsingDeclarations: LexicographicNumeric
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeJsonColon: false
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterForeachMacros: true
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
AfterPlacementOperator: true
|
||||
AfterRequiresInClause: false
|
||||
AfterRequiresInExpression: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceBeforeSquareBrackets: false
|
||||
SpaceInEmptyBlock: true
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: Never
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParens: Never
|
||||
SpacesInParensOptions:
|
||||
ExceptDoubleParentheses: false
|
||||
InCStyleCasts: false
|
||||
InConditionalStatements: false
|
||||
InEmptyParentheses: false
|
||||
Other: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Latest
|
||||
StatementAttributeLikeMacros:
|
||||
- Q_EMIT
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TableGenBreakInsideDAGArg: DontBreak
|
||||
TabWidth: 4
|
||||
UseTab: Always
|
||||
VerilogBreakBetweenInstancePorts: true
|
||||
WhitespaceSensitiveMacros:
|
||||
- BOOST_PP_STRINGIZE
|
||||
- CF_SWIFT_NAME
|
||||
- NS_SWIFT_NAME
|
||||
- PP_STRINGIZE
|
||||
- STRINGIZE
|
||||
WrapNamespaceBodyWithEmptyLines: Leave
|
||||
...
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.28)
|
||||
|
||||
project(CHANGEME
|
||||
project(collision_crisis
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ Clone this repository and cd into it.
|
|||
|
||||
Run `just set-project-name {name}` in the project root directory.
|
||||
|
||||
It's best to then commit the script's changes to git.
|
||||
It's best to then commit the script's changes to git immediately.
|
||||
|
||||
Run `just generate` and `just build`, this should output a binary named {name} (as given for `set-project-name`) in the `bin/` subdirectory of the project root.
|
||||
|
||||
If you want to share code between projects using the same template, make the shared code into a [git submodule(1)](https://linux.die.net/man/1/git-submodule) and add it as it's own cmake directory.
|
||||
If you want to share code between projects using the same template, make the shared code into a [git submodule(1)](https://linux.die.net/man/1/git-submodule) and add it as it's own cmake directory.
|
||||
|
|
|
|||
9
justfile
9
justfile
|
|
@ -1,3 +1,6 @@
|
|||
run:
|
||||
cd bin/ && ./collision_crisis
|
||||
|
||||
build:
|
||||
cmake --build build
|
||||
|
||||
|
|
@ -8,7 +11,7 @@ clean:
|
|||
rm -rf build/* bin/* build/.* bin/.*
|
||||
|
||||
set-project-name projectname:
|
||||
sed -i "s/CHANGEME/{{projectname}}/g" ./CMakeLists.txt ./src/CMakeLists.txt ./justfile .kdev4/*
|
||||
mv ./src/CHANGEME.cpp ./src/{{projectname}}.cpp
|
||||
sed -i "s/CHANGEME.cpp/{{projectname}}.cpp/g" ./justfile
|
||||
sed -i "s/collision_crisis/{{projectname}}/g" ./CMakeLists.txt ./src/CMakeLists.txt ./justfile
|
||||
mv ./src/collision_crisis.cpp ./src/{{projectname}}.cpp
|
||||
sed -i "s/collision_crisis.cpp/{{projectname}}.cpp/g" ./justfile
|
||||
rm -rf build bin
|
||||
|
|
|
|||
140
src/Balls.hpp
Normal file
140
src/Balls.hpp
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
struct Ball {
|
||||
sf::CircleShape shape;
|
||||
sf::Vector2f velocity;
|
||||
|
||||
Ball(float x, float y, float radius, sf::Color color, float vx, float vy) {
|
||||
shape.setRadius(radius);
|
||||
shape.setPosition(sf::Vector2f(x, y));
|
||||
shape.setFillColor(color);
|
||||
shape.setOrigin(sf::Vector2f(radius, radius)); // Center origin
|
||||
velocity = sf::Vector2f(vx, vy);
|
||||
}
|
||||
};
|
||||
|
||||
class BallGame {
|
||||
private:
|
||||
// Create balls
|
||||
std::vector<Ball> balls;
|
||||
std::random_device rd;
|
||||
std::mt19937 gen;
|
||||
std::uniform_real_distribution<float> posDist;
|
||||
std::uniform_real_distribution<float> velDist;
|
||||
std::uniform_int_distribution<int> colorDist;
|
||||
std::uniform_real_distribution<float> radiusDist;
|
||||
|
||||
public:
|
||||
BallGame::BallGame() {
|
||||
gen = std::mt19937(rd());
|
||||
posDist = std::uniform_real_distribution<float>(5.0f, 795.0f);
|
||||
velDist = std::uniform_real_distribution<float>(-200.0f, 200.0f);
|
||||
colorDist = std::uniform_int_distribution<int>(0, 255);
|
||||
radiusDist = std::uniform_real_distribution<float>(2.5f, 2.5f);
|
||||
|
||||
// Generate random balls
|
||||
for (int i = 0; i < 2500; ++i) {
|
||||
sf::Color randomColor(colorDist(gen), colorDist(gen), colorDist(gen));
|
||||
balls.emplace_back(
|
||||
posDist(gen), posDist(gen), // position
|
||||
radiusDist(gen), // radius
|
||||
randomColor, // color
|
||||
velDist(gen), velDist(gen) // velocity
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateBalls(const sf::Vector2u& windowSize, float deltaTime) {
|
||||
// Update positions
|
||||
for (auto& ball : balls) {
|
||||
ball.shape.move(ball.velocity * deltaTime);
|
||||
}
|
||||
|
||||
// Handle ball-to-ball collisions
|
||||
for (size_t i = 0; i < balls.size(); ++i) {
|
||||
for (size_t j = i + 1; j < balls.size(); ++j) {
|
||||
Ball& ball1 = balls[i];
|
||||
Ball& ball2 = balls[j];
|
||||
|
||||
sf::Vector2f pos1 = ball1.shape.getPosition();
|
||||
sf::Vector2f pos2 = ball2.shape.getPosition();
|
||||
float radius1 = ball1.shape.getRadius();
|
||||
float radius2 = ball2.shape.getRadius();
|
||||
|
||||
// Calculate distance between centers
|
||||
sf::Vector2f delta = pos2 - pos1;
|
||||
float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
float minDistance = radius1 + radius2;
|
||||
|
||||
if (distance < minDistance && distance > 0) {
|
||||
// Normalize collision vector
|
||||
sf::Vector2f normal = delta / distance;
|
||||
|
||||
// Separate balls to prevent overlap
|
||||
float overlap = minDistance - distance;
|
||||
sf::Vector2f separation = normal * (overlap * 0.5f);
|
||||
ball1.shape.setPosition(pos1 - separation);
|
||||
ball2.shape.setPosition(pos2 + separation);
|
||||
|
||||
// Calculate relative velocity
|
||||
sf::Vector2f relativeVel = ball2.velocity - ball1.velocity;
|
||||
float velAlongNormal = relativeVel.x * normal.x + relativeVel.y * normal.y;
|
||||
|
||||
// Don't resolve if velocities are separating
|
||||
if (velAlongNormal > 0) continue;
|
||||
|
||||
// Apply collision response (elastic collision)
|
||||
float restitution = 0.0f; // Bounce factor (0 = no bounce, 1 = perfect bounce)
|
||||
float impulse = -(1 + restitution) * velAlongNormal;
|
||||
|
||||
// Assume equal mass for simplicity
|
||||
sf::Vector2f impulseVector = impulse * normal;
|
||||
ball1.velocity -= impulseVector;
|
||||
ball2.velocity += impulseVector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle wall collisions
|
||||
for (auto& ball : balls) {
|
||||
sf::Vector2f pos = ball.shape.getPosition();
|
||||
float radius = ball.shape.getRadius();
|
||||
|
||||
// Bounce off walls
|
||||
if (pos.x - radius <= 0 || pos.x + radius >= windowSize.x) {
|
||||
ball.velocity.x = -ball.velocity.x;
|
||||
// Clamp position to prevent sticking
|
||||
if (pos.x - radius <= 0) {
|
||||
ball.shape.setPosition(sf::Vector2f(radius, pos.y));
|
||||
}
|
||||
else {
|
||||
ball.shape.setPosition(sf::Vector2f(windowSize.x - radius, pos.y));
|
||||
}
|
||||
}
|
||||
|
||||
if (pos.y - radius <= 0 || pos.y + radius >= windowSize.y) {
|
||||
ball.velocity.y = -ball.velocity.y;
|
||||
// Clamp position to prevent sticking
|
||||
if (pos.y - radius <= 0) {
|
||||
ball.shape.setPosition(sf::Vector2f(pos.x, radius));
|
||||
}
|
||||
else {
|
||||
ball.shape.setPosition(sf::Vector2f(pos.x, windowSize.y - radius));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawBalls( sf::RenderWindow& window ) const
|
||||
{
|
||||
for (const auto& ball : balls) {
|
||||
window.draw(ball.shape);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
#include "SFML/Graphics/RectangleShape.hpp"
|
||||
#include "SFML/Graphics/RenderStates.hpp"
|
||||
#include "defs.h"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <imgui-SFML.h>
|
||||
#include <imgui.h>
|
||||
#include <print>
|
||||
|
||||
void configure(AppConfig &config) {
|
||||
config.window_title = "MYPROJECT";
|
||||
config.frame_rate_limit = std::nullopt;
|
||||
config.vsync = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
ImGui::GetIO().ConfigFlags |= (
|
||||
ImGuiConfigFlags_NavEnableKeyboard
|
||||
| ImGuiConfigFlags_NavEnableGamepad
|
||||
| ImGuiConfigFlags_DockingEnable
|
||||
);
|
||||
}
|
||||
|
||||
void loop(double delta) {
|
||||
}
|
||||
|
||||
void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states) {
|
||||
static sf::RectangleShape rect{ { 300, 300 } };
|
||||
sf::RenderStates n_states{states};
|
||||
n_states.transform.translate({ 20, 20 });
|
||||
target.draw(rect, n_states);
|
||||
}
|
||||
|
||||
void draw_main_menu_bar() {
|
||||
if (ImGui::BeginMenu("Edit")) {
|
||||
if (ImGui::MenuItem("Menu item!")) {
|
||||
std::println("Wahooo!!!");
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
void draw_gui() {
|
||||
// draw your GUI
|
||||
ImGui::DockSpaceOverViewport(0, NULL, ImGuiDockNodeFlags_PassthruCentralNode);
|
||||
if (ImGui::Begin("My Window")) {
|
||||
ImGui::Text("A window with text and a button!!");
|
||||
if (ImGui::Button("My Button")) {
|
||||
std::println("Yipeeee");
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
if (ImGui::Begin("Second Window :O")) {
|
||||
ImGui::Text("A window with text!");
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
file(GLOB_RECURSE SOURCE_FILES *.cpp)
|
||||
|
||||
add_executable(CHANGEME ${SOURCE_FILES})
|
||||
add_executable(collision_crisis ${SOURCE_FILES})
|
||||
|
||||
set_target_properties(CHANGEME PROPERTIES
|
||||
set_target_properties(collision_crisis PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../bin/"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../lib/"
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../lib/"
|
||||
)
|
||||
|
||||
target_link_libraries(CHANGEME
|
||||
target_link_libraries(collision_crisis
|
||||
PUBLIC
|
||||
ImGui-SFML::ImGui-SFML
|
||||
)
|
||||
|
|
|
|||
72
src/collision_crisis.cpp
Normal file
72
src/collision_crisis.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include "defs.h"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <SFML/Window/Event.hpp>
|
||||
#include <imgui-SFML.h>
|
||||
#include <imgui.h>
|
||||
#include <print>
|
||||
|
||||
void configure(AppConfig &config) {
|
||||
config.window_title = "CHANGEME";
|
||||
config.frame_rate_limit = std::nullopt;
|
||||
config.vsync = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
ImGui::GetIO().ConfigFlags |= (ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_DockingEnable);
|
||||
sf::View view{get_window().getView()};
|
||||
view.setCenter({0.f, 0.f});
|
||||
set_render_view(view);
|
||||
}
|
||||
|
||||
void handle_input_event(sf::Event const &event) {
|
||||
}
|
||||
|
||||
void handle_window_event(sf::Event const &event) {
|
||||
if (sf::Event::Resized const *resized{event.getIf<sf::Event::Resized>()}) {
|
||||
sf::View view{get_window().getView()};
|
||||
view.setSize({static_cast<float>(resized->size.x), static_cast<float>(resized->size.y)});
|
||||
set_render_view(view);
|
||||
}
|
||||
}
|
||||
|
||||
void loop(double delta) {
|
||||
}
|
||||
|
||||
void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states) {
|
||||
static sf::RectangleShape rect{{300, 300}};
|
||||
sf::RenderStates n_states{states};
|
||||
n_states.transform.translate({-150, -150});
|
||||
target.draw(rect, n_states);
|
||||
}
|
||||
|
||||
void draw_main_menu_bar() {
|
||||
if (ImGui::BeginMenu("Edit")) {
|
||||
if (ImGui::MenuItem("Menu item!")) {
|
||||
std::println("Wahooo!!!");
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
void draw_gui() {
|
||||
// draw your GUI
|
||||
ImGui::DockSpaceOverViewport(0, NULL, ImGuiDockNodeFlags_PassthruCentralNode);
|
||||
if (ImGui::Begin("My Window")) {
|
||||
ImGui::Text("A window with text and a button!!");
|
||||
if (ImGui::Button("My Button")) {
|
||||
std::println("Yipeeee");
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
if (ImGui::Begin("Second Window :O")) {
|
||||
ImGui::Text("A window with text!");
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
}
|
||||
14
src/defs.h
14
src/defs.h
|
|
@ -1,10 +1,13 @@
|
|||
#ifndef LLCS_DEFS_H
|
||||
#define LLCS_DEFS_H
|
||||
|
||||
#include <optional>
|
||||
#include <SFML/System/String.hpp>
|
||||
#include <SFML/Window/VideoMode.hpp>
|
||||
#include "SFML/Graphics/View.hpp"
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/System/String.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <SFML/Window/VideoMode.hpp>
|
||||
#include <optional>
|
||||
|
||||
struct AppConfig {
|
||||
sf::String window_title{ "UNNAMED WINDOW" };
|
||||
|
|
@ -14,4 +17,9 @@ struct AppConfig {
|
|||
std::optional<int> frame_rate_limit{std::nullopt};
|
||||
};
|
||||
|
||||
extern AppConfig const &get_application_configuration();
|
||||
extern sf::Time const &get_delta_time();
|
||||
extern sf::RenderWindow const &get_window();
|
||||
extern void set_render_view(sf::View const &view);
|
||||
|
||||
#endif // !LLCS_DEFS_H
|
||||
|
|
|
|||
68
src/main.cpp
68
src/main.cpp
|
|
@ -5,7 +5,8 @@
|
|||
#include <imgui.h>
|
||||
|
||||
static sf::RenderWindow window{};
|
||||
static sf::Clock deltaClock{};
|
||||
static sf::Clock delta_clock{};
|
||||
static sf::Time delta_time{};
|
||||
static AppConfig cfg{};
|
||||
static sf::View view{};
|
||||
|
||||
|
|
@ -26,17 +27,44 @@ void initialize_imgui() {
|
|||
exit(-1);
|
||||
}
|
||||
|
||||
extern void handle_input_event(sf::Event const &evt);
|
||||
extern void handle_window_event(sf::Event const &evt);
|
||||
|
||||
void try_handle_input_event(sf::Event const &event) {
|
||||
bool const is_input_event{
|
||||
event.is<sf::Event::JoystickButtonPressed>() || event.is<sf::Event::JoystickButtonReleased>() ||
|
||||
event.is<sf::Event::JoystickMoved>() ||
|
||||
event.is<sf::Event::JoystickConnected>() || event.is<sf::Event::JoystickDisconnected>() ||
|
||||
event.is<sf::Event::KeyPressed>() || event.is<sf::Event::KeyReleased>() ||
|
||||
event.is<sf::Event::TouchBegan>() || event.is<sf::Event::TouchEnded>() ||
|
||||
event.is<sf::Event::TouchMoved>() ||
|
||||
event.is<sf::Event::MouseButtonPressed>() || event.is<sf::Event::MouseButtonReleased>() ||
|
||||
event.is<sf::Event::MouseMoved>() || event.is<sf::Event::MouseMovedRaw>()
|
||||
};
|
||||
if (is_input_event) {
|
||||
handle_input_event(event);
|
||||
}
|
||||
}
|
||||
|
||||
void try_handle_window_event(sf::Event const &event) {
|
||||
bool const is_window_event{
|
||||
event.is<sf::Event::FocusGained>() ||
|
||||
event.is<sf::Event::FocusLost>() ||
|
||||
event.is<sf::Event::Resized>()
|
||||
};
|
||||
if (is_window_event) {
|
||||
handle_window_event(event);
|
||||
}
|
||||
}
|
||||
|
||||
void poll_events() {
|
||||
while (std::optional const event{ window.pollEvent() })
|
||||
{
|
||||
ImGui::SFML::ProcessEvent(window, *event);
|
||||
// "close requested" event: we close the window
|
||||
if (event->is<sf::Event::Closed>()) {
|
||||
window.close();
|
||||
} else if (sf::Event::Resized const *resized{ event->getIf<sf::Event::Resized>()}) {
|
||||
view.setSize({ static_cast<float>(resized->size.x), static_cast<float>(resized->size.y) });
|
||||
window.setView(view);
|
||||
}
|
||||
try_handle_input_event(*event);
|
||||
try_handle_window_event(*event);
|
||||
|
||||
if (event->is<sf::Event::Closed>()) { window.close(); }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -45,6 +73,7 @@ extern void loop(double delta);
|
|||
extern void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states);
|
||||
extern void draw_main_menu_bar(void);
|
||||
extern void draw_gui(void);
|
||||
extern void shutdown(void);
|
||||
|
||||
int main() {
|
||||
initialize_window();
|
||||
|
|
@ -54,9 +83,9 @@ int main() {
|
|||
// Event Polling
|
||||
poll_events();
|
||||
// Update
|
||||
sf::Time const deltaTime{ deltaClock.restart() };
|
||||
ImGui::SFML::Update(window, deltaTime);
|
||||
loop(deltaTime.asSeconds());
|
||||
delta_time = delta_clock.restart();
|
||||
ImGui::SFML::Update(window, delta_time);
|
||||
loop(delta_time.asSeconds());
|
||||
if(ImGui::BeginMainMenuBar()) {
|
||||
draw_main_menu_bar();
|
||||
ImGui::EndMainMenuBar();
|
||||
|
|
@ -68,6 +97,23 @@ int main() {
|
|||
ImGui::SFML::Render(window);
|
||||
window.display();
|
||||
}
|
||||
shutdown();
|
||||
ImGui::SFML::Shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
||||
AppConfig const &get_application_configuration() {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
sf::Time const &get_delta_time() {
|
||||
return delta_time;
|
||||
}
|
||||
|
||||
sf::RenderWindow const &get_window() {
|
||||
return window;
|
||||
}
|
||||
|
||||
void set_render_view(sf::View const &view) {
|
||||
window.setView(view);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue