Problem with Font.Draw
Author |
Message |
Ninja
|
Posted: Sat Jun 10, 2006 11:17 am Post subject: Problem with Font.Draw |
|
|
Hey guys. Im making the final project for my comsci class (Grade 11). What im trying to do here is import a file called welcome.txt and have it displayed on the intro screen with Font.Draw command. Everytime i try to do that, it takes the text and puts it on the screen but its overlapping on top of every word. Is it even possible to use Font.Draw with reading from a file, or should i just type the whole "welcome" message out manually. Thanx
code: | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Rock and roll database program %
% Written by : Akshay Singh %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------Procedure to draw the menu_screen%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
import GUI
var winID : int
winID := Window.Open ("graphics:800;600, nobuttonbar")
var picture : int
var text : string
var font1 : int
font1 := Font.New ("Comicsans:14:bold,italic")
procedure menu_screen (var picture : int, var text : string)
var menu_screen : int := Pic.FileNew ("coolpic copy.jpg")
Pic.Draw (menu_screen, 0, 0, 0)
var number : array 1 .. 14 of string
var filenumber : int
var x : int := 0
open : filenumber, "welcome.txt", get
assert filenumber > 0
loop
get : filenumber, skip
exit when eof (filenumber)
x := x + 1
get : filenumber, number (x)
end loop
for y : 1 .. 14
Font.Draw (number (y), 100, 100, font1, red)
%put " ", number (y) ..
end for
end menu_screen
menu_screen (picture, text)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%---Creating the quitting button using the GUI module%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var quitBtn : int := GUI.CreateButton (200, 50, 0, "Quit", GUI.Quit)
GUI.SetColour (quitBtn, 102)
loop
exit when GUI.ProcessEvent
end loop
Window.Close (winID) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Sat Jun 10, 2006 1:41 pm Post subject: (No subject) |
|
|
Of course it's going to overlap. Look carefully at this line:
code: |
Font.Draw (number (y), 100, 100, font1, red)
|
The (x, y) coordinates that you're drawing at is (100, 100). It's constant. It never changes.
Instead, you should decrease the y coordinates as you move through all your lines.
code: |
Font.Draw (number (y), 100, 200 - 16 * y, font1, red)
|
200 - 16 * y will move you down by 16 pixels each line. Assuming your font is size 14, that's a good amount to move down each line.
Note that you do not need your array, number. You could read the line in from the file and draw it immediately. Also, your array, number has a hardcoded range of 1 .. 14. This is a terrible habit. You know the number of lines in your welcome file. If you changed it, or if you wanted this code to work with any other welcome file, your code would not work. |
|
|
|
|
![](images/spacer.gif) |
Ninja
|
Posted: Sun Jun 11, 2006 6:27 pm Post subject: (No subject) |
|
|
thanx bro. i got it workin now, heres the modified code
code: | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Rock and roll database program %
% Written by : Akshay Singh %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------Declaring global variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
import GUI
var winID : int
winID := Window.Open ("graphics:800;600, nobuttonbar")
var picture : int
var text : string
var font1 : int
var offset : int
font1 := Font.New ("Comicsans:14:bold,italic")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------Procedure to draw the menu_screen%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure menu_screen (var picture : int, var text : string)
var menu_screen : int := Pic.FileNew ("coolpic copy2.jpg")
Pic.Draw (menu_screen, 0, 0, 0)
var number : array 1 .. 7 of string
var filenumber : int
var x : int := 0
open : filenumber, "welcome.txt", get
assert filenumber > 0
loop
get : filenumber, skip
exit when eof (filenumber)
x := x + 1
get : filenumber, number (x)
end loop
offset := 0
for y : 1 .. 7
Font.Draw (number (y), offset, 430, font1, 40)
offset := offset + Font.Width (number (y) + " " , font1)
end for
end menu_screen
menu_screen (picture, text)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%---Creating the quitting button using the GUI module%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var quitBtn : int := GUI.CreateButton (200, 50, 0, "Quit", GUI.Quit)
GUI.SetColour (quitBtn, 102)
loop
exit when GUI.ProcessEvent
end loop
Window.Close (winID)
|
|
|
|
|
|
![](images/spacer.gif) |
Bored
![](http://compsci.ca/v3/uploads/user_avatars/7678827054c0296ac1832b.gif)
|
Posted: Sun Jun 11, 2006 8:36 pm Post subject: (No subject) |
|
|
Your still harcoding the number of lines.
code: | var number : array 1 .. 7 of string |
What if you changed the welcome file and added a 8th line, you would get an error. Now that's ok just change the number array to 8. But what if you wanted this to work with any welcome file, and they all had different amounts of lines. Well there's two soloutions as Cervantes mentioned. The first, considering you only use the lines in one spot is to replace the number array with a single variable and write the number as soon as you read it. This would also reduce the amount of code as you remove the for loop and put the code in the loop above. the other answer is a flexible array. You could then each loop increas the size by onw and have the for loop run from 1 to upper (number). |
|
|
|
|
![](images/spacer.gif) |
|
|