Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » vline, etc.

This thread is locked; no one can reply to it. rss feed Print
vline, etc.
julian_boolean
Member #8,201
January 2007

Yet another text-related question! I'm trying to create a caret and position it on the screen for input, but vline only has one x position. I tried different ones (like "line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);" but all this does is make the program crash for some reason (probably because I don't know what I'm doing. :))

void cTextField::set_text_stats(int xl1, int yl1, int xl2, int yl2, int x, int y, int l)
{
    x1_line = xl1;
    y1_line = yl1;
    x2_line = xl2;
    y2_line = yl2;
    x_text = x;
    y_text = y;
    l_text = l;
}

line(buffer, caret *x1_line, y1_line, x2_line, y2_line, WHITE);

textout_ex(buffer, font, edittext.c_str(), x_text, y_text, WHITE, -1);

This function is suppose to allow me to position both the text and the caret, and also change the length of the caret and how far ahead it should be placed when inputting.

(Link to the text input code I'm using) http://www.gamedev.net/reference/articles/article2130.asp

Kauhiz
Member #4,798
July 2004

Umm... what's with the function? Why not just use xl1 etc. directly? And what are you using for those values?

Quote:

but vline only has one x position.

::)

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

julian_boolean
Member #8,201
January 2007

8, 8, 8, 18, 0, 10, 0

The function is suppose to set the height of the caret, set where it should be placed when you insert a character, set the x coordinate for it, set the x and y coordinates of the text itself, and the max length of the text (which I havn't got around to doing.)

Evert
Member #794
November 2000
avatar

Quote:

vline only has one x position.

It's a vertical line. What other x position would you want to specifiy for it?

julian_boolean
Member #8,201
January 2007

Well in 23yrold3yrold's code hes using the x value for inputting text. If I wanted to move the caret horizontally (to make it start at a specific x position) and keep the same value when inputting (8) then I'd need a second x.

Tobias Dammers
Member #2,604
August 2002
avatar

Your question is not at all text-related. Drawing a caret has nothing to do with text, and if your call to line() crashes, my guess would be you a) try to draw to a bitmap that doesn't exist or b) draw outside a bitmap's area while clipping is switched off or c) dereference invalid pointer in the process of passing arguments to line().
I think you are over-complicating the matter. As far as I can tell, what you are trying to do is display some text on screen, and a caret at the current edit position. The minimum variable set you'd need for that would be:

unsigned caret_pos; // the character index into the string the caret is at
int txt_x;
int txt_y;
std::string txt;

You might want to throw in an extra caret_height int.
Then your drawing code might look something like this:

textout_ex(backbuffer, font, txt.c_str(), txt_x, txt_y, WHITE, -1); // please make sure WHITE is defined as something meaningful!
vline(backbuffer, txt_x + text_length(font, txt.substr(0, caret_pos)), txt_y, txt_y + caret_height, WHITE);

That's pretty much it.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

julian_boolean
Member #8,201
January 2007

Got an error compiling that said: cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `2' to `int text_length(const FONT*, const char*)'

Not really sure what it means.. Too tired to think straight. :P

Kauhiz
Member #4,798
July 2004

What are you using as caret_pos?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Hano Wair
Member #5,243
November 2004
avatar

Quote:

Got an error compiling that said: cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `2' to `int text_length(const FONT*, const char*)'

Not really sure what it means.. Too tired to think straight. :P

The text_length() function expects a const char* as its second argument but you are giving it a std::string instead, use:

std::string txt;
// ...
text_length(font, txt.c_str());

julian_boolean
Member #8,201
January 2007

Figured it out ;) .. Not sure what you guys were trying to tell me, but thanks anywho. :) Here's what I got:

vline(buffer, textx + caret *8, texty - 2, texty + 8, WHITE);

Now I just need to the max text length and I should be all set!

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

textx + caret *8

What if I go wild and change the font? :P Just use text_length...

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

julian_boolean
Member #8,201
January 2007

I don't know how to change the font anyway (as much as I'd like to) and besides I tried to do the text_length thing and it didn't work for me. :(

Evert
Member #794
November 2000
avatar

Quote:

I don't know how to change the font anyway (as much as I'd like to)

What did you think the FONT* parameter to textout and textprintf() was supposed tobe used for? ;)

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

besides I tried to do the text_length thing and it didn't work for me

See the above post. Allegro is a C library, so it expects a C-style string. Thus the .c_str()...

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Go to: