• Advertisement
Sign in to follow this  

Unity Cursor obliterating the background.

This topic is 1971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello community,

First, let me specify my language and Graphics library: C++, Allegro 4.

So I've come across this minor issue whereby I have my cursor drawing on-screen. I am using Allegro's show mouse and letting the graphics library handling its draws. My 'riveting' Tic-Tac-Toe was fine, up until the point I decided to draw the current players' shape underneath the Arrow cursor, the reason for this being better visual clarity of exactly whose turn it is.

When I show the shape moving with the Mouse coordinates, the shape trails and draws all over the background. Then I made a function which saves a portion of the Buffer (minus the cursor/player-shape cursor), and redraws it after the buffer has been drawn to screen, thereby restoring the obliterated portion.

This works perfectly when I modify the mouse position using Integer Coordinates that are increased/decreased on various key-presses. I have even tested it at differing speeds of +=1, 15, 75 and each time the background is preserved. When I move the cursor without key-presses (with Allegro's standard mouse movement) the background is still shown to be obliterated, but in shudders. It's difficult to explain so please find an image attached.

I can't seem to understand why that no matter how fast the key-movement compared to mouse-movement happens, that the mouse has this effect.

Below is the code that saves the Bitmap portion;
[source lang="cpp"]BITMAP* GameEngine::f_SaveBitmapPortion(BITMAP* p_Source)
// Create a temporary bitmap
///BITMAP* temp;
temp = Graphics::f_CreateBitmap(p_Source->w, p_Source->h);

// TODO: (remove a_Buffer in favour of a second parameter, renamed to source
// and change the name of p_Source to regionSize.
blit(a_Buffer, temp,
// Blit from the Buffer(source), using the coordinate location to 'cut-away' said area
mouse_x - (p_Source->w / 2),
mouse_y - (p_Source->h / 2),
// to the Temp Bitmap(dest) coordinates at 0, 0
0, 0,

// return the temp bitmap
return temp;
And below this is the code that shows where in the Main-loop that it is saved/re-drawn to screen;
[source lang="cpp"]// Drawing
if(g.f_GetShapeCursor() != NULL)
portionBG = g.f_SaveBitmapPortion(g.f_GetShapeCursor());
blit(g.f_GetBuffer(), screen, 0, 0, 0, 0, screen->w, screen->h);

if(g.f_GetShapeCursor() != NULL)
blit(portionBG, g.f_GetBuffer(), 0, 0, mouse_x - (g.f_GetShapeCursor()->w / 2), mouse_y - (g.f_GetShapeCursor()->h / 2), g.f_GetShapeCursor()->w, g.f_GetShapeCursor()->h);
Thanks very much for taking your time in reading this and any input is greatly appreciated.



P.S. Regarding the image, this portion of the buffer drawn isn't trailing all the time, it only happens once every several loops, so it looks like the Grid lines are being 'push-pulled' all over the place.

Share this post

Link to post
Share on other sites

I was hoping that there would have been a response by now. Apologies for the late bump but I have had no access to the net since the time of posting, and I wasn't sure whether or not to re-post as a new topic. Again, any help is greatly appreciated.



Share this post

Link to post
Share on other sites
The simple solution is to re-draw the whole screen every frame. That will resolve the issue of your BG getting obilerated, and, I promise, you won't see any performance hit with it either.

Share this post

Link to post
Share on other sites
Try rounding up your floating point numbers (mouse_x - (GetShapeCursor()->w / 2)) and casting to an integer such as static_cast<int>(ceil(mouse_x - (GetShapeCursor()->w / 2))). Blitting a fraction of a pixel sounds like it could be the case for the issue you're experiencing.

Share this post

Link to post
Share on other sites
Thanks for your responses guys. I do redraw every single frame, I was using the BG copy function to remove the cursor 'trail' that was being left behind. Guessing there might be slight performance issues from Buffer/Mini-BG drawing on every frame?

I have solved the issue I was having by calling a clear_bitmap(buffer) at the end of the loop. It must have been the problem that I never noticed before as the grid is always drawn in the same position.

I am curious though, where would the floating point number come from if my shape cursor width, and the Allegro mouse X is of type Integer, to begin with?

Share this post

Link to post
Share on other sites
oh man I should have totally picked up on the lack of a buffer clear. That very same issue happened with me years ago when I first tried writing a side-scrolling 2D shooter and had trails of characters being stretched all over the place.

The floating point number exists as an assumption. I assumed the mouse coordinates were floats. Since you're just working with ints there then you should be golden.

Glad to see you got this sorted out.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By NDraskovic
      Hey guys,
      I have a really weird problem. I'm trying to get some data from a REST service. I'm using the following code:
      private void GetTheScores() { UnityWebRequest GetCommand = UnityWebRequest.Get(url); UnityWebRequestAsyncOperation operation = GetCommand.SendWebRequest(); if (!operation.webRequest.isNetworkError) { ResultsContainer rez = JsonUtility.FromJson<ResultsContainer>(operation.webRequest.downloadHandler.text); Debug.Log("Text: " + operation.webRequest.downloadHandler.text); } } The problem is that when I'm in Unity's editor, the request doesn't return anything (operation.webRequest.downloadHandler.text is empty, the Debug.Log command just prints "Text: "), but when I enter the debug mode and insert a breakpoint on that line, then it returns the text properly. Does anyone have an idea why is this happening?
      The real problem I'm trying to solve is that when I receive the text, I can't get the data from the JSON. The markup is really simple:
      [{"id":1,"name":"Player1"},{"id":2,"name":"Player2"}] and I have an object that should accept that data:
      [System.Serializable] public class ResultScript { public int id; public string name; } There is also a class that should accept the array of these objects (which the JSON is returning):
      [System.Serializable] public class ResultsContainer { public ResultScript[] results; } But when I run the code (in the debug mode, to get any result) I get an error: ArgumentException: JSON must represent an object type. I've googled it but none of the proposed solutions work for me.
      Also (regardless if I'm in the debug mode or not) when I try to do some string operations like removing or adding characters to the GET result, the functions return an empty string as a result
      Can you help me with any of these problems?
      Thank you
    • By nihitori
      The Emotional Music Vol. I pack focuses on beautiful and esoteric orchestral music, capable of creating truly emotive and intimate moods. It features detailed chamber strings, cello and piano as the main instruments, resulting in a subtle and elegant sound never before heard in video game royalty-free music assets.

      The pack includes 5 original tracks, as well as a total of 47 loops based on these tracks (long loops for simple use and short loops for custom / complex music layering).

      Unity Asset Store link: https://www.assetstore.unity3d.com/en/#!/content/107032
      Unreal Engine Marketplace link: https://www.unrealengine.com/marketplace/emotional-music-vol-i

      A 15 seconds preview of each main track is available on Soundcloud:
    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      -Skills in Unity
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
      Additional Project Info:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
    • By RoKabium Games
      Custom coffee mugs have arrived... More caffeine!
      Have a great weekend everyone! 
      #gamedev #indiedev #sama #caffeine
  • Advertisement