AutoSkill C++ Raylib Province Class Implementation
Implement a C++ class using Raylib to manage, draw, save, and load 2D polygon provinces (map regions) with support for world coordinates, file I/O, and interactive editing.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/c-raylib-province-class-implementation" ~/.claude/skills/ecnu-icalk-autoskill-c-raylib-province-class-implementation && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/c-raylib-province-class-implementation/SKILL.mdsource content
C++ Raylib Province Class Implementation
Implement a C++ class using Raylib to manage, draw, save, and load 2D polygon provinces (map regions) with support for world coordinates, file I/O, and interactive editing.
Prompt
Role & Objective
You are a C++ developer using the Raylib library. Your task is to implement a
Province class that manages 2D polygon regions (provinces) for a map editor. The class must handle drawing, user interaction (adding/deleting points), and file persistence.
Communication & Style Preferences
- Use standard C++ practices and Raylib API conventions.
- Ensure type safety, specifically converting between integer coordinates and Raylib's floating-point Vector2.
- Code should be compatible with a Camera2D system for world coordinates.
Operational Rules & Constraints
-
Class Structure:
- Define a class
.Province - Include a nested struct
withPoint
andint x
.int y - Private members:
: Stores points for the province currently being drawn.std::vector<Point> points
: Stores all completed provinces.std::vector<std::vector<Point>> provinces
- Define a class
-
Method Implementations:
:drawProvince(Color fillColor, bool drawStroke, Color strokeColor, float strokeThickness)- Iterates through the
vector.provinces - Converts
(int) toPoint
(float) for compatibility with Raylib drawing functions.Vector2 - Calls a polygon drawing function (e.g.,
or Raylib'sDrawPolyEx
fan) to render the fill and stroke.DrawTriangle
- Iterates through the
:drawPoints()- Runs a loop until the Enter key is pressed.
- On left mouse click, adds the current mouse position (converted to world coordinates) to the
vector.points - On Enter, pushes the current
vector intopoints
and clearsprovinces
.points
:deletePoints()- Removes the last element from the
vector if it is not empty.points
- Removes the last element from the
:savePointsToFile(std::string filename)- Opens the file in append mode.
- For each province in
:provinces- Write X coordinates separated by ';' on one line.
- Write Y coordinates separated by ';' on the next line.
- Write an empty line as a separator.
:loadProvincesFromFile(std::string filename)- Reads the file line by line.
- Parses pairs of lines (X coords, Y coords) to reconstruct
vectors.Point - Populates the
vector.provinces
:deleteProvince(Vector2 mousePoint)- (Placeholder) Logic to identify and remove a province if the mouse point is inside it.
-
Coordinate System:
- Use
to get mouse coordinates for adding points.GetScreenToWorld2D(GetMousePosition(), camera) - Ensure all drawing happens inside
andBeginMode2D(camera)
.EndMode2D()
must be called once at the start of the frame, beforeClearBackground()
.BeginMode2D
- Use
-
Type Conversion:
- When converting
toProvince::Point
, useVector2
for x and y components.static_cast<float> - When generating random colors using
, cast the result toGetRandomValue
to avoid narrowing warnings:unsigned char
.static_cast<unsigned char>(GetRandomValue(0, 255))
- When converting
-
File Format:
- Provinces are saved as blocks of 3 lines:
- X coordinates (e.g.,
)100;200;300; - Y coordinates (e.g.,
)100;200;300; - Empty line (separator)
- X coordinates (e.g.,
- Provinces are saved as blocks of 3 lines:
Anti-Patterns
- Do not define functions inside other functions (e.g., nested function definitions).
- Do not mix
andProvince::Point
types in the same vector without conversion.Vector2 - Do not call
inside the 2D camera mode block.ClearBackground - Do not use blocking loops (like
) inside the main render loop if it halts the application; handle input event-by-event in the main loop instead.while (!IsKeyPressed(KEY_ENTER))
Interaction Workflow
- Initialize
object.Province - In the main game loop:
- Handle Input: Check for mouse clicks to add points or Backspace to delete points.
- Update: (If applicable)
- Draw: Clear screen -> Begin 2D Mode -> Draw Background -> Call
-> End 2D Mode.province.drawProvince()
Triggers
- Write the Province class in C++ and raylib
- Implement drawProvince function with Raylib
- Save and load provinces from text file
- Draw polygons using world coordinates
- Handle mouse input for drawing points