Skip to content

Change x,y->Point w,h->Extent#40

Merged
Flamefire merged 11 commits into
Return-To-The-Roots:masterfrom
morganchristiansson:xy-point-extent
Jun 25, 2026
Merged

Change x,y->Point w,h->Extent#40
Flamefire merged 11 commits into
Return-To-The-Roots:masterfrom
morganchristiansson:xy-point-extent

Conversation

@morganchristiansson

Copy link
Copy Markdown
Contributor

Based on this comment #37 (review)

Replace raw x,y and w,h parameters with Position and Extent types across
CMap, CSurface, CControlContainer, CButton, CPicture, CTextfield, and
CFont to make X/Y vs W/H semantics explicit and prevent copy-paste errors.

  • CMap/CSurface: vertex/modification methods take Position pos instead
    of separate int x, int y / int VertexX, VertexY / Uint16 MouseX, MouseY
  • CControlContainer: addButton/addText/addPicture/addStaticPicture/addTextfield
    take Point16 pos / Extent16 size (consistent with addSelectBox)
  • CButton/CPicture/CTextfield/CFont: constructors take Point16 pos /
    Extent16 size with internal storage renamed to pos_/size_
  • All call sites in callbacks.cpp and CDebug.cpp updated

@morganchristiansson morganchristiansson force-pushed the xy-point-extent branch 4 times, most recently from a2e78ad to 276f709 Compare June 18, 2026 13:02
Comment thread CIO/CButton.cpp Outdated
Comment on lines +16 to +17
this->pos_ = pos;
this->size_ = size;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put those directly into the constructors initializer list for new code.

In this case it doesnt change any relevant order.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread CIO/CButton.cpp Outdated
{
// cursor is on the button (and mouse button not pressed while moving on the button)
if((motion.x >= x_) && (motion.x < x_ + w) && (motion.y >= y_) && (motion.y < y_ + h))
if((motion.x >= pos_.x) && (motion.x < pos_.x + size_.x) && (motion.y >= pos_.y) && (motion.y < pos_.y + size_.y))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we also had something like this in the code IsPointInExtent or so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsPointInExtent is now used

Comment thread CIO/CButton.cpp Outdated
}
} else if(button_text)
CFont::writeText(Surf_Button, button_text, (int)w / 2, (int)((h - 11) / 2), FontSize::Medium, button_text_color,
CFont::writeText(Surf_Button, button_text, Position((int)size_.x / 2, (int)((size_.y - 11) / 2)), FontSize::Medium, button_text_color,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C cast still necessary? if so, replace by static_cast instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast removed.

Comment thread CIO/CPicture.h Outdated
#pragma once

#include "SdlSurface.h"
#include "defines.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed here?

@morganchristiansson morganchristiansson Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed because CPicture.h uses Point16 and Extent16 types for the new pos_ and size_ member
variables:

Point16 pos_;
Extent16 size_;

These typedefs are defined in defines.h:

using Point16 = Point<Sint16>;
using Extent16 = Point<Uint16>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can now be removed again, can't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment thread callbacks.cpp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can't be rendered on github - what the heck happened?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me. But it's very large diff :-D

Comment thread CIO/CButton.cpp Outdated
}
} else if(button_text)
CFont::writeText(Surf_Button, button_text, Position((int)size_.x / 2, (int)((size_.y - 11) / 2)),
CFont::writeText(Surf_Button, button_text, Position(static_cast<int>(size_.x) / 2, static_cast<int>(size_.y - 11) / 2),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the cast required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast removed.
AI loves adding casts unfortunately.

Comment thread callbacks.cpp Outdated
Comment thread CIO/CButton.cpp Outdated
{
// cursor is on the button (and mouse button not pressed while moving on the button)
if((motion.x >= x_) && (motion.x < x_ + w) && (motion.y >= y_) && (motion.y < y_ + h))
if(IsPointInRect(Position(motion.x, motion.y), Rect(Position(pos_), Extent(size_))))

@Flamefire Flamefire Jun 18, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast (Position(pos_))? Should work directly, doesn't it?
Similar below?

If it cannot convert the Point16 to Position automatically then I'd say we should store them as Position/Extent. I mentioned that earlier that in almost all cases the default is fine instead of the sized-variants. As this seems to be an automatic conversion there is no real need to change that too, but if it causes issues like here(?) then it makes sense to change it at least for this class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CButton.cpp: size_ is Extent16 = Point. Uint16 implicitly promotes to int for arithmetic (standard
integral promotion), and Position(int, int) accepts the promoted values directly. So Position(size_.x / 2,
(size_.y - 11) / 2) works fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

@morganchristiansson morganchristiansson Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to go ahead and change ALL Point16 to Position in the codebase and remove Point16 type?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least where it requires extra work like here.
I'm not sure if there is any use of the sized Point types, maybe in some file operations. But generally the ~4 extra bytes per control/window don't matter (anymore), so yes it makes sense to replace them to make them easier to handle and reason about

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok went all the way and pushed usage of Point/Extent in UI mainly all the way to the C SDL functions.

@morganchristiansson

Copy link
Copy Markdown
Contributor Author

Can we get this one out of the way?

It's a pure refactor that doesn't make any changes and there's no reason for it to sit in queue.

I stupidly based another branch on this thinking it would be quick to merge.

@Flamefire

Copy link
Copy Markdown
Member

There is an open question

@Flamefire Flamefire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better, thanks. 2 things:

  • Offsets should be signed. It could use a typedef like using Offset = Point<int> for clarity.
  • Lot's of introduced casts. Double-check where they are actually required. Many look uneccessary to me.

Comment thread CIO/CFile.cpp Outdated
// read start addresses
for(int y = 0; y < shadowArray->h; y++)
CHECK_READ(libendian::le_read_us(&starts[y], fp));
for(Position pos{0, 0}; pos.y < shadowArray->h; pos.y++)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't like Position in for loop?
Was originally going to leave for loops alone, but there's a lot of x, y usage in them and this is actually nice I think.

But hmm this is CFile, not UI. Maybe should not convert CFile. There was bug with unitialized pos from changes here.
And why is CFile in same folder as all UI classes anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this loop you loop over y only, so that's not a position

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok reverted the y-only loop. Kept Positio for x,y loops.

Comment thread CIO/CButton.cpp Outdated
// position in the Surface 'Surf_Button'
Uint16 pos_x = 0;
Uint16 pos_y = 0;
unsigned pos_x = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not convert those too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread CIO/CPicture.h Outdated
#pragma once

#include "SdlSurface.h"
#include "defines.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can now be removed again, can't it?

Comment thread CIO/CSelectBox.cpp Outdated
// position in the Surface 'Surf_Button'
Uint16 pos_x = 0;
Uint16 pos_y = 0;
unsigned pos_x = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted

Comment thread CIO/CTextfield.cpp Outdated
@@ -340,45 +340,45 @@ bool CTextfield::render()
// black frame is left and up
// draw vertical line
pos_x = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those variable values can be inlined (Position(0, y))

@morganchristiansson morganchristiansson Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inlined to expressions

-                pos_x = size_.x - 1;
                 for(unsigned y = 0; y < size_.y; y++)
-                    CSurface::DrawPixel_RGB(Surf_Text, Position(pos_x, y), 0, 0, 0);
+                    CSurface::DrawPixel_RGB(Surf_Text, Position(size_.x - 1, y), 0, 0, 0);

Comment thread CSurface.cpp Outdated
@morganchristiansson

Copy link
Copy Markdown
Contributor Author

(force pushed to apply clang-format to previously pushed commit)

@morganchristiansson

Copy link
Copy Markdown
Contributor Author

Please can we keep pushing this one as it will cause conflicts with other PRs

@Flamefire Flamefire merged commit 2ef8ed5 into Return-To-The-Roots:master Jun 25, 2026
1 check passed
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.

3 participants