[SOLVED] DrawingSurface.DrawPixel not precise on the Y axis?

Started by LostTrainDude, Wed 25/03/2015 22:05:43

Previous topic - Next topic

LostTrainDude

I was trying to create a "space map" with randomly placed planets (as pixels, not as sprites) on it.

My goal is to make the mouse show which planet is the player looking at when moving onto it. It kinda works, but for some reason the pixel on the screen and the stored coordinates don't match.
The problem seems to occur only on the Y axis, anyway, which appears to be exactly +40 pixels from the actual pixel drawn onto the screen.



This is the whole code I've written.
I don't know if it's an useful information but I'm using AGS 3.3.3 and a 320x200 resolution.

Galaxy Map Header
Code: ags
//GalaxyMap.ash

struct PlanetStruct {
    int coord[2]; //for storing X and Y coordinates
    DrawingSurface* s;
};

import PlanetStruct Planet[5];


Galaxy Map Script
Code: ags
//GalaxyMap.asc

PlanetStruct Planet[5];

function repeatedly_execute_always()
{
    // This is the script that shows both the current mouse.x and mouse.y
    // AND a Display text when hitting a planet's coordinates

    String x = String.Format("%d, %d", mouse.x,  mouse.y);
    player.SayBackground(x);
    if (player.Room == 1) {
        int n = 0;
	while (n < 5) {
            if (mouse.x == Planet[n].coord[0] && mouse.y == Planet[n].coord[1]) Display("Planet: %d", n);
            n++;
	}
    }
}

export Planet;


Room Script
Code: ags
//room1.asc

// Creates a given amount of planets, randomly placed over the screen
// Stores their X and Y coordinates and displays them next to them
function PlanetForming(int totalPlanet)
{
    int n = 0;
    while (n < totalPlanet) {
	Planet[n].s = Room.GetDrawingSurfaceForBackground();
        // Probably ultra-spaghetti. I was trying to keep the pixels a little far from the corners of the screen.
	int rX = Random(Room.Width-10)+10;
	int rY = Random(Room.Height-10)+10; // Same as above
	
	Planet[n].s.DrawingColor = 15;
	Planet[n].s.DrawPixel(rX, rY);
	Planet[n].s.Release();
	
	Planet[n].coord[0] = rX; // Store the X coordinate of the drawn pixel
	Planet[n].coord[1] = rY; // Store the Y coordinate of the drawn pixel

        // Next to each planet, write it's current x and y coordrinates
	String st = String.Format("%d, %d", rX, rY);
	DrawingSurface* t = Room.GetDrawingSurfaceForBackground();
	t.DrawingColor = 15;
	t.DrawStringWrapped(rX, rY, 25, eFontfntTiny, eAlignCentre, st);
	t.Release();
	n++;
    }	
}

function room_Load()
{
    PlanetForming(5);
}


With no clue whatsoever, I've also tried using the UseHighResCoordinates, but without luck.

Thanks in advance, even more if the matter is easy to solve and I'm just bad at this :D
"We do not stop playing because we grow old, we grow old because we stop playing."

Snarky

Interesting. It looks OK in theory... Might be that 320x200 somehow uses a subsection of a 320x240 canvas, and for some reason the bottom, not the top part. If I were you, I would test by rawdrawing a line from 0,0 to 320,200 (or rather, 319,199) and see how it appears.

The other thing I would address in this code is the DrawingSurface* in the struct and in the PlanetForming() function. There's absolutely no reason for that to be part of the PlanetStruct, or for you to grab and release GetDrawingSurfaceForBackground() twice for each planet. It's always going to be the same! Just grab it once before you enter the loop and release it afterwards, and don't store it as part of your data at all.

LostTrainDude

Quote from: Snarky on Wed 25/03/2015 22:29:33
Interesting. It looks OK in theory... Might be that 320x200 somehow uses a subsection of a 320x240 canvas, and for some reason the bottom, not the top part. If I were you, I would test by rawdrawing a line from 0,0 to 320,200 (or rather, 319,199) and see how it appears.
You've probably nailed it! This is from 0,0 to 319,199.


Quote from: Snarky on Wed 25/03/2015 22:29:33
The other thing I would address in this code is the DrawingSurface* in the struct and in the PlanetForming() function. There's absolutely no reason for that to be part of the PlanetStruct, or for you to grab and release GetDrawingSurfaceForBackground() twice for each planet. It's always going to be the same! Just grab it once before you enter the loop and release it afterwards, and don't store it as part of your data at all.

For this other matter I was actually trying to test if I was in any way able to create "hotspots" off of the drawing surfaces (so that each planet would have had their own "personal" one), because later on I would have liked to create larger "planets" with rectangles rather then pixels... But that just shows how naive I am (laugh)
"We do not stop playing because we grow old, we grow old because we stop playing."

Gilbert

#3
I was just lazy to check out all the codes, but maybe you try putting Display("%d", Room.Height); (or just check this in the Editor) so to check what the height of the room actually is. It is possible that for some reason the room dimension is 320x240 (note that this is not related to the display resolution, which is 320x200 in your case). if that is the case then re-import a background of dimension 320x200 to that room and see if it makes a difference.

P.S. I think the default blank room template is of dimension 320x240, so whenever you create a new room its dimension is initially set to 320x240 regardless of what resolution your game is set to. It is a good practice to always import a background to a newly created room, even though it's supposed to be completely black.

Monsieur OUXX

Quote from: Gilbert on Thu 26/03/2015 03:29:28
It is a good practice to always import a background to a newly created room, even though it's supposed to be completely black.

Yes, never ever use the default black background, it causes so many issues! This is another side effect of the room-that-has-no-background-image issue that was discussed in other threads. It is known for causing drawing issues with all DynamicSprite-related functions (because the color depth and alpha settings are unknown).
Always immediately import your own background.

 

LostTrainDude

Now I feel kinda stupid, actually (roll).
I did create a 320x200 black image but probably forgot to import it as a background for the room in the first place.

I tried it again and it worked perfectly at 320x200.
Thanks a million for your help :D
"We do not stop playing because we grow old, we grow old because we stop playing."

SMF spam blocked by CleanTalk