Skip to content

[WIP] Refactor constructors to use initializer lists in raylib project#1

Closed
Copilot wants to merge 1 commit intomasterfrom
copilot/refactor-constructors-initializer-lists
Closed

[WIP] Refactor constructors to use initializer lists in raylib project#1
Copilot wants to merge 1 commit intomasterfrom
copilot/refactor-constructors-initializer-lists

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 2, 2025

Coding agent has begun work on Refactor and fix raylib test project; move constructors to initializer lists and will update this pull request as work progresses.

Original prompt

Kannst du mir alle Konstruktoren auf die Initializer List umstellen

TITLE: Refactor and fix raylib test project; move constructors to initializer lists

USER INTENT:

  • Make the project correct and clean: fix build/link errors and constructor-initialization bugs.
  • Refactor code so constructors use initializer lists (user later requested converting all constructors to initializer lists).
  • Keep code readable and avoid introducing new problems.

TASK DESCRIPTION:

  • Fix linker error caused by not linking raylib.
  • Implement ball movement and paddle collision (velocity updates, lateral impulse on paddle hit).
  • Refactor code into Ball, Paddle, Game classes with headers in include/ and implementations in src/.
  • Fix incorrect constructor initialization (comma operator bug / members assigned in body) by using proper initializer lists.
  • Decide on destructors: ensure none are missing when classes own resources; add if needed.
  • Final requested change: convert all constructors to use initializer lists.

EXISTING (what has been done so far):

  • Linker problem diagnosed: undefined references to raylib functions; CMake fix proposed (find_package(raylib) / pkg-config fallback).
  • A working single-file example of ball + paddle physics was provided and then converted into a refactored multi-file layout.
  • Refactored files (provided by assistant):
    • include/Ball.h
    • src/Ball.cpp
    • include/Paddle.h
    • src/Paddle.cpp
    • include/Game.h
    • src/Game.cpp
    • src/main.cpp
    • CMakeLists.txt (snippet showing raylib linking)
  • Multiple fixes applied to Game constructor initialization:
    • Fixed comma-operator bug when initializing paddle in initializer list.
    • Moved paddle and ball initialization from constructor body to initializer list.
  • Decision documented about destructors: not required unless classes own raylib resources (e.g., Texture2D) or raw pointers. Examples shown for adding destructor if owning Texture2D.

PENDING (what still needs to be done):

  • Convert all constructors across all classes to initializer lists (user explicitly requested this). Specifically:
    • Ball constructor(s) in include/Ball.h / src/Ball.cpp
    • Paddle constructor(s) in include/Paddle.h / src/Paddle.cpp
    • Game constructor already converted in later fixes, but verify consistency and correctness.
    • Any other constructors (if present) should be updated.
  • Verify and test that CMake actually finds and links raylib in the user's environment (install libraylib-dev or ensure find_package succeeds).
  • Optionally add destructors if future changes introduce owned raylib resources.
  • Run and test the program to ensure no new runtime issues.

CODE STATE: list of files discussed and important snippets/diffs

  1. CMakeLists.txt (linking raylib)
  • Before (missing link -> caused undefined references):
    • main.cpp compiled but executable not linked with raylib
  • After (suggested/used fix):
find_package(raylib QUIET)

add_executable(raylib_test main.cpp)

if(raylib_FOUND)
    target_link_libraries(raylib_test PRIVATE raylib)
else()
    find_package(PkgConfig REQUIRED)
    pkg_search_module(RAYLIB REQUIRED raylib)
    target_include_directories(raylib_test PRIVATE ${RAYLIB_INCLUDE_DIRS})
    target_link_libraries(raylib_test PRIVATE ${RAYLIB_LIBRARIES})
endif()
  1. Single-file game example (main.cpp) — provided earlier (ball + paddle)
  • Shows ball movement, collisions, lateral impulse on paddle hit and kept increased speed (no normalization).
  • No constructor-related changes here; used as reference for behavior.
  1. Refactored layout — relevant constructor-related snippets

Game constructor: bug and fix

  • Problem: comma operator / improper initializer caused wrong initialization
  • Before (buggy initialization — comma operator interpreted; or members assigned in body)
// Bad — comma operator or assignment in body
Game::Game(int screenW, int screenH)
    : screenWidth(screenW),
      screenHeight(screenH),
      paddle((static_cast<float>(screenWidth) / 2 - 50, static_cast<float>(screenHeight) - 30)),
      ball(10.0f, Vector2{ screenW/2.0f, screenH/2.0f }, Vector2{ 200.0f, 200.0f }) {}
  • After (fixed; use brace-init for Vector2)
// Good — initializer list with brace-initialized Vector2
Game::Game(int screenW, int screenH)
    : screenWidth(screenW),
      screenHeight(screenH),
      paddle({static_cast<float>(screenW) / 2 - 50.0f, static_cast<float>(screenH) - 30.0f}),
      ball(10.0f, {static_cast<float>(screenW) / 2, static_cast<float>(screenH) / 2}, {200.0f, 200.0f})
{}

Paddle constructor example (before / after conversion to initializer list)

  • Before (possible version assigning in body):
Paddle::Paddle(int w, int h, Vector2 pos, float spd) {
    position = pos;
    width = w;
    height = h;
    speed = spd;
}
  • After (use initializer list):
Paddle::Paddle(int w, int h, Vector2 pos, float spd)
    : position(pos), width(w), height(h), speed(spd) {}

Ball constructor example (before / ...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants