Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

878731 Posts in 32935 Topics- by 24343 Members - Latest Member: Good Enough Games

May 22, 2013, 01:31:44 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)The grumpy old programmer room
Pages: 1 ... 164 165 [166] 167 168 ... 275
Print
Author Topic: The grumpy old programmer room  (Read 306527 times)
pgil
Guest
« Reply #2475 on: July 20, 2011, 07:14:24 AM »

I decided to rewrite my WordWrap script.  I figure I'll understand it better if I do it myself:

Code:
public static function WordWrap(txt:String, wid:int, font:String):String {
//txt: the text to wordwrap
//wid: The maximum width of the text in pixels
//font: The font to use when measuring width (assumes font size of 12)
var buffer:String;
var i:int;

for (i = 0; i <= txt.length; i += 1) {
buffer += txt.substr(i, 1); // Add txt to buffer, one character at a time.
if (GetWidth(buffer, font) > wid) { // If the text is widerthan given "wid".
i = buffer.lastIndexOf(" "); // move back to the last space.
buffer = buffer.slice(0, i); // delete everything after it.
buffer += "\n"; // Add a line break.
}

}
return buffer; //return complete text with line breaks.
}
}

Two problems:

1. This adds "null" to the beginning of the text.

2. It still removes words at the beginning of some lines.

So this:
Quote
See that dark figure creeping out of the woods?  That is the wolf. 

Becomes this:
Quote
nullSee that dark figure creeping out
he woods?  That is the wolf.
Logged
TobiasW
Level 7
**


This can only end brilliantly!


View Profile WWW Email
« Reply #2476 on: July 20, 2011, 07:24:45 AM »

Code:
var buffer:String = "";
should fix the "null" thing. Also probably the word-removing problem.

More effective (assuming GetWidth takes some cycles) would probably be to split txt in words (by splitting it with ' ' as seperator) and then always add whole words, only going to the next word when the word was successfully added because it fits or because the current line was empty. (The "or the current line was empty" thing is needed when you have a VERY long word, longer than your line width. Like "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschafturlaubsvertretungsantragsbewilligung". See what I just did there?)
« Last Edit: July 20, 2011, 07:31:13 AM by TobiasW » Logged

pgil
Guest
« Reply #2477 on: July 20, 2011, 07:39:51 AM »

Code:
var buffer:String = "";
should fix the "null" thing. Also probably the word-removing problem.

whoops.. Yeah, that should have been obvious. 

huh.... It looks like doing that actually solved both problems. I still haven't tested it with words that are longer than the maximum width, but since there's no user-written text in my game that shouldn't be a problem.

Thanks!
Logged
Sean A.
Level 7
**



View Profile Email
« Reply #2478 on: July 20, 2011, 07:47:21 AM »

Yeah I am assuming the null thing wasn't factored in when calculating where to line break but once it was added it pushed the line passed the max so it deleted some of the letters because if you look at your example there are 4 characters missing so I think that could have been it.
Logged
TheLastBanana
Level 7
**


thelastbanana@hotmail.com
View Profile WWW Email
« Reply #2479 on: July 21, 2011, 01:54:49 PM »

Just spent a day and a half trying to figure out why my debugger was reporting a segfault before main(). I had assumed that it was in a static object somewhere in my engine, and continued removing bits and pieces one at a time to see what was the problem. I got it down to the point that this was the only thing in my project:
Code:
#include <iostream>
int main() {
return 0;
}
That was still giving me a segfault, unless I removed iostream.
Then I discovered, an hour later, that my firewall has, out of the blue, decided to start blocking the debugger. Facepalm
Logged

rivon
Level 10
*****



View Profile
« Reply #2480 on: July 22, 2011, 10:00:44 AM »

Firewall? Blocking debugger? How's that possible? Firewall blocks internet connections right? Is your debugger searching for something on Google or what?
Logged
TheLastBanana
Level 7
**


thelastbanana@hotmail.com
View Profile WWW Email
« Reply #2481 on: July 22, 2011, 11:08:40 AM »

COMODO Firewall has a feature called "Defense+" which works as a sort of resident shield. You could say it wasn't really the firewall part of it that did it, but that's why it confused me - I had unblocked the debugger in my antivirus program, but hadn't realized that my firewall had a component that was blocking it, too. Kind of a stupid mistake, but oh well.
Logged

rivon
Level 10
*****



View Profile
« Reply #2482 on: July 22, 2011, 02:14:37 PM »

Yeah, that explains it.
Logged
mechacrash
Level 1
*


It's almost like I'm actually being productive!


View Profile Email
« Reply #2483 on: July 22, 2011, 07:17:00 PM »

After spending a large amount of time dealing with the non-programming side of my life, I finally return to my project to discover bugs I never fixed before...

Problem here is, it's been so long that now I don't even understand my own code -_-

Time to start some heavy backtracking
Logged

Sean A.
Level 7
**



View Profile Email
« Reply #2484 on: July 22, 2011, 07:33:47 PM »

After spending a large amount of time dealing with the non-programming side of my life, I finally return to my project to discover bugs I never fixed before...

Problem here is, it's been so long that now I don't even understand my own code -_-

Time to start some heavy backtracking
That's why I always try to comment my code as much as I can.
Logged
mechacrash
Level 1
*


It's almost like I'm actually being productive!


View Profile Email
« Reply #2485 on: July 22, 2011, 07:40:05 PM »

haha... I guess I'll learn these lessons the hard way.

In all honesty, I havn't commented ANY code... though ill probably do it when it finally comes to uploading it online or something
Logged

TobiasW
Level 7
**


This can only end brilliantly!


View Profile WWW Email
« Reply #2486 on: July 23, 2011, 04:32:58 AM »

In all honesty, I havn't commented ANY code... though ill probably do it when it finally comes to uploading it online or something
Is it much code? Because if you don't have a very very strong motivation to do it later (not just "putting it up for a bunch of unknown people at the internet"), you'll probably skip it later if you don't do it now.
Logged

Skomakar'n
Level 10
*****


Vąutah


View Profile WWW Email
« Reply #2487 on: July 23, 2011, 06:30:22 AM »

One of my mottos is that it can be okay for code to be a little ugly as long as one makes sure to comment the gruesome parts extensively. That can save the day sometimes. I always comment my code unless I'm just trying something out.
Logged

mak gam
Geisha Novia: Out now!
Bottoms Up!: Devlog

Royal Railway on Twitter.

Adam Emil
zacaj
Level 3
***


void main()


View Profile WWW Email
« Reply #2488 on: July 23, 2011, 07:51:54 AM »

In all honesty, I havn't commented ANY code... though ill probably do it when it finally comes to uploading it online or something
Is it much code? Because if you don't have a very very strong motivation to do it later (not just "putting it up for a bunch of unknown people at the internet"), you'll probably skip it later if you don't do it now.
I always put my code on my hacked kindle when I go on vacation, and comment it there
Logged

My twitter: @zacaj_

Quote from: mcc
Well let's just take a look at this "getting started" page and see--
Quote
Download and install cmake
Noooooooo
Nix
Level 10
*****



View Profile
« Reply #2489 on: July 23, 2011, 10:17:12 AM »

My uncommented code is so clean a five year old could understand it
Logged
Pages: 1 ... 164 165 [166] 167 168 ... 275
Print
Jump to:  

Theme orange-lt created by panic