Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / output file writing and input file reading Help

Author
Message
Dimo
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location:
Posted: 13th Jun 2010 00:07
This is what I got, but for some reason the last mouse inputs draws the line of the screen, I thing it has to do in the writting process but I can't figure it out.


//This program allows the user to click points on the window
//screen until the spacebar key is pressed. The program saves
//the mouses X and Y coordinates to an output file as the
//user clicks the mouse. When the user presses the spacebar, the
//output file is closed and reopened as an input file. The coordinates
//are read and lines are drawn from the users mouse clicks.
#include "DarkGDK.h"

//Declare Constants
const int FILE_NUM = 1;
const int REFRESH_RATE = 60;

//Function Prototype
void setUp();
void openInputFile();

/***********************************************************
DarkGDK Function
***********************************************************/
void DarkGDK()
{
//Declare variable for mouses X and Y position.
int mouseX, mouseY;

//Variable to hold the starting X and Y coordinate
//of the first mouse click.
int xCoordinate, yCoordinate;

//Variables to hold the lines starting and ending
//points.
int startX, startY, endX, endY;

//Perform the set Up function
setUp();

while(LoopGDK() && !dbSpaceKey()) //Perform loop until the user pressed the Esc Key or the SpaceBar Key.
{
//If the left mouse button is fully clicked
//assign the X and Y coordinates values to mouseX
//and MouseY and write the coordinates in the output file.
if(dbMouseClick() == 1)
{
//Set the mouse pointer's coordiantes.
mouseX = dbMouseX();
mouseY = dbMouseY();

//Write the mouse pointer's X and Y coordinates to the output file.
dbWriteFile(FILE_NUM, mouseX);
dbWriteFile(FILE_NUM, mouseY);
}
//Refresh the Screen.
dbSync();
}

//Enable automatic sync rate.
dbSyncOff();

//Close The Output File.
dbCloseFile(FILE_NUM);

//Clear the screen.
dbCLS();

//Perform the openInputFile function to check for the
//input file and if exists open it.
openInputFile();

//If the End of the input file has not reached, set the
//mouse pointer's x and y coordinates from the input file.
if(!dbFileEnd(FILE_NUM))
{
//Set the mouse pointers X and Y coordinates from the input file
xCoordinate = dbReadFile(FILE_NUM);
yCoordinate = dbReadFile(FILE_NUM);
}

//If the end of the input file has not reached, set the starting X and Y
//coordinates of the line and draw the line connecting the users clicked points.
while(!dbFileEnd(FILE_NUM))
{
//Set the lines starting and ending line coordinates.
startX = xCoordinate;
startY = yCoordinate;
endX = dbReadFile(FILE_NUM);
endY = dbReadFile(FILE_NUM);

//For some reason the !dbFileEnd producing another statement, I think
//it has to do in the writting part of the coordinates.dat file.
//Because when used with the bool function, both points have 480 > results.
if(endX < 640)
{
//Draw the line based on users mouse clicks
dbLine(startX, startY, endX, endY);

//Set the next starting coordinates to the ending coordinates of
//the previous line drawn.
xCoordinate = endX;
yCoordinate = endY;
}
}

//Close the Input File.
dbCloseFile(FILE_NUM);

//Prompt the user to press any key to exit the program.
dbCenterText(319, 440, "Press Any Key To Exit The Program.");

//Wait for the user to press a key.
dbWaitKey();
}

/*********************************************************************
The setUp function checks if the coordinates.dat file exists and if
it does deletes it and opens a new output file. Sets the Title screen
to "Connecting the Dots", Prompts the user, and disables automatic
sync rate and sets the refresh rate to 60.
**********************************************************************/
void setUp() //Function Header
{
/*Check if the file coordinates.dat exists, if the .dat file exists
delete it, and open an output file to store the mouses X and Y
Coordinates*/
if(dbFileExist("coordinates.dat"))
{
//If the MouseCoordinates.dat exists delete it.
dbDeleteFile("coordinates.dat");

//Open a output file to store mouse clicked
//X and Y coordinates.
dbOpenToWrite(FILE_NUM, "coordinates.dat");
}
else //If the file does not exist open output file
{
//Open a output file to store mouse clicked
//X and Y coordinates.
dbOpenToWrite(FILE_NUM, "coordinates.dat");
}

//Set the window Title
dbSetWindowTitle("Connect The Dots");

//Prompt the user to click the mouse anywhere on the screen
dbCenterText(319, 40, "Click The Mouse Anywhere On The Screen.");

//Prompt the user to press the space bar when he is done.
dbCenterText(319, 449, "Press The Space Bar When You Are Done.");

//Disable the automatic sync rate.
dbSyncOn();

//Set the refresh rate to 60.
dbSyncRate(REFRESH_RATE);
}

/**************************************************************************
The openInputFile function checks if the coordinates.dat input file exists
and opens it if it does. If the file does not exist Prompts the user an
Error message stating the file does not exist..
**************************************************************************/
void openInputFile() //Function Header.
{
//Check if the file exists.
if(dbFileExist("coordinates.dat") )
{
//Open the input file.
dbOpenToRead(FILE_NUM, "coordinates.dat");
}
else //the file does not exist
{
//Print an error message.
dbPrint("Error! The file does not exist.");
}
}

Dimo
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 13th Jun 2010 09:22
I don't understand the problem you are having. I've downloaded the above code and it seems to be working just fine for me. Could you explain your problem a little better?

Also, you really need to place the code in code blocks...

JTK
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 13th Jun 2010 09:47 Edited at: 13th Jun 2010 09:49
I understand the problem and know why it's happening as well. You had to insert this check into the program:



because if you don't, you get an additional line after the last point. The reason is simple. dbFileEnd does not know if the file has ended until it tries to read the next data from it. When you read the last pair of coordinates from the file, dbFileEnd will still return false (file has not ended) because the last reading was successful. Only when you try to read again, then it recognises that there are no more data, but then it's too late, you already read the bogus coordinates.

The easiest way to solve this is to exchange your 640 test for another dbFileEnd condition. You really shouldn't check the value that you read back, instead you should check the end of the file. However, if you know for sure that you read an impossible value, that is a good check as well, so the 640 test can even remain there as it is now.

I was thinking for a while if the loop could be optimized in any way so that this extra check is not necessary, but there seems to be no elegant solution. You simply have to check after reading the value, not before.

One more thing. To optimize the code, you should eliminate at least two pairs of variables. You use mouseX, mouseY, xCoordinate, yCoordinate, startX, startY, endX, endY and in reality, the most number of values you have to store at the same time is two pairs of values. It is perfectly possible to write this program to use only startX, startY, endX, endY and eliminate the other two pairs of variables.

P.S. and yes, please insert the code into code tags next time (use the button above the posting text area before and after the code). It preserves the indentation so the code will be much easier to read and you get a nice pushbutton as well to open/close the code part.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 13th Jun 2010 10:07
P.S. one more thing. dbMouseClick() does NOT mean that the mouse is "fully clicked", as you say in your remarks. This function is unfortunately named in a misleading way. It simply means that the mouse button is held down. This means that for one click, you will write more than one value into the file. The longer you hold the mouse button for a click, the more values (with the same coordinates) you will write out. (You can easily check this by counting the number of values you write out, for me it was 5-6 values per mouseclick.) This is inefficient. You should introduce a boolean variable to keep track of the mouse button status and write into the file only when the button changes from "held down" to "not held down".

Login to post a reply

Server time is: 2024-11-19 23:11:11
Your offset time is: 2024-11-19 23:11:11