Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:06:56+00:00 2026-06-10T07:06:56+00:00

I am making a game (for fun) and it has a scrolling screen. The

  • 0

I am making a game (for fun) and it has a scrolling screen. The following bit of code updates the position:

screen_x += screen_scroll_x;
if(screen_x < 0)
    screen_x = 0;

if(screen_x > map_width - screen_width)
    screen_x = map_width - screen_width;

I’m struggling to choose the correct integer type for screen_x. If it is signed, screen_x > map_width - screen_width induces a warning “comparison between unsigned and signed”. If it is unsigned, if(screen_x < 0) fails (with creative results) as screen_x can never be negative.

map_width is compared to the length of a string when loading the map and so if we make map_width signed the problem is shifted to that portion of code.

Many people have said that you should use the correct integer. They also say pay attention to your warnings and it bugs me that I do get warnings (get too many and you stop paying attention to them).

What would be the ideal solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:06:58+00:00Added an answer on June 10, 2026 at 7:06 am

    You should not compare directly arbitrary unsigned and signed ints if you’re interested in mathematically sensible comparison. That’s because C(++)’s arithmetic is not your regular math arithmetic.

    In a C(++) expression involving a signed int and an unsigned int, the signed int is first converted to an unsigned int and then the operation is performed (+, *, <, etc).

    To correctly compare a signed int and an unsigned int you should take into account C(++)’s “arithmetic rules” and those type/value conversions mandated by the programming language and invisible to the eye of the uninitiated.

    So, you could compare the two like this:

    /* returns -1 if s < u,
       returns 0 if s == u,
       returns 1 if s > u */
    int CompareSignedUnsigned(int s, unsigned u)
    {
      if (s < 0) return -1; // negative is always smaller than 0 or positive
      if (s < u) return -1; // obvious
      if (s > u) return 1;  // obvious
      return 0; // obvious
    }
    

    As for using the right kind of integer, that’s a good advice, but sometimes one size doesn’t fit all. And on-screen coordinates is one such area, where it can be more than reasonable to have coordinates signed.

    Imagine, for example, you want a routine to render a box on the screen and that you want to be able to draw a box moving across the screen, starting outside of the screen (moving into it) and ending outside (moving out).

    If you choose the API like this:

    void DrawBox(unsigned x, unsigned y, unsigned width, unsigned height, color c);
    

    the user of this function will have to do some extra math when drawing a portion of that box, when a part is on the screen and another is off the screen. All because there’s simply no way to tell the function that the original/uncropped box really isn’t all on the screen.

    Now, if you choose this one instead:

    void DrawBox(int x, int y, unsigned width, unsigned height, color c);
    

    and move that extra cropping math inside of the function, suddenly the user of this function is enabled to write very simple code. It could be as simple as:

    for (int x = -100; x < SCREEN_WIDTH + 100, x++)
      DrawBox(x, SCREEN_HEIGHT / 2, 100, 50, GREEN);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While making a little Pong game in C++ OpenGL, I decided it'd be fun
Started making a game. Here's some of my code. package games.tribe.screens; import games.tribe.model.World; import
I am making a game that has its own level editor. The editor simply
I'm making a game using javascript + canvas. I use the code below to
I'm making a web game for fun with lots of forms that post data
I was playing the Javascript game with somebody and we were having fun making
I'm making a role playing game just for fun and to learn more about
I am making a role playing game for fun and attempting to use TDD
I'm making a pretty simple game just for fun/practice but I still want to
I am making a game that has several classes and each one do some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.