View previous topic :: View next topic |
Author |
Message |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jul 14, 2009 10:31 am Post subject: [C] Centering text on G15 LCD? |
|
|
Hello there.
I've been messing a bit with my G15 LCD and C, but I've ran into a problem.
How would I center a text on a 160x43 LCD? The middle of that must obviously be (80,21.5), but if I write text there, that point will be the start of the text.
So how would I calculate the pixel width of a string/text in C?
The code I was using for writing text:
Code: | void WriteText(unsigned char stringOut[], int x, int y)
{
g15r_renderString(canvas, stringOut, 0, G15_TEXT_MED, x, y);
/* This is to remind me.
Width = 160
Height = 43
*/
} |
I'm using libG15, documentation can be found here:
http://www.g15tools.com/docs-g15r/libg15render_8h.html
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Jul 14, 2009 11:54 am Post subject: |
|
|
i'm not sure it can be done perfectly for every computer unless you use a font where each character has a set width. in which case you can get the width of the whole string, divide by two, minus the offset from centre and draw there. other than that, i'm not aware of a way
i was trying to do something similar ages ago. which was to make the window caption of a program centred instead of left aligned. never found a good way..
|
|
Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Tue Jul 14, 2009 11:58 am Post subject: |
|
|
Slugsnack wrote: | i'm not sure it can be done perfectly for every computer unless you use a font where each character has a set width. in which case you can get the width of the whole string, divide by two, minus the offset from centre and draw there. other than that, i'm not aware of a way
i was trying to do something similar ages ago. which was to make the window caption of a program centred instead of left aligned. never found a good way.. | Get window width / 2 = middle k
|
|
Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jul 14, 2009 12:02 pm Post subject: |
|
|
; wrote: | Slugsnack wrote: | i'm not sure it can be done perfectly for every computer unless you use a font where each character has a set width. in which case you can get the width of the whole string, divide by two, minus the offset from centre and draw there. other than that, i'm not aware of a way
i was trying to do something similar ages ago. which was to make the window caption of a program centred instead of left aligned. never found a good way.. | Get window width / 2 = middle k |
GNU wrote: | but if I write text there, that point will be the start of the text. |
Same thing...
EDIT: Another question as well:
Does anybody know how to make a simple XML parser in C? I'm trying to make a ETQuake Wars stats viewer for my G15, and I would be able to get all information I needed on this site (Not my account).
I'd like to know how to parse various information from that XML file, if anybody has got a simple solution.
|
|
Back to top |
|
 |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Tue Jul 14, 2009 12:12 pm Post subject: |
|
|
I should try to program some stuff for my G13....
Try something like this:
Code: |
#define SCREEN_WIDTH 160
#define SCREEN_HEIGHT 43
char text[] = "Hello, World.";
int middle = (SCREEN_WIDTH/2) - (sizeof(text)/2); |
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 14, 2009 12:28 pm Post subject: |
|
|
Chaosis13 wrote: | Try something like this: | Better yet, if you're going to write a function like PrintStringCenter(char* s) use strlen, because sizeof(char*) will return the size of the pointer. Something like this could be done: Code: | const int ScreenWidth = 80;
void PrintStringCenter(char* s)
{
int c = (ScreenWidth-strlen(s))/2;
while( c-- )
printf(" ");
printf("%s", s);
} | Or if your actual print function supports coordinates, c is the starting point.
EDIT: Oh wait, you can print anywhere, and those are pixel coordinates. Your screen isn't 160 chars in width, but 160 pixels, right? Then you need to know about the font you're using. It's width etc. Monospace font suggested :) Soo.. Something like this: Code: | const int ScreenWidthPx = 160;
const int ScreenHeightPx = 60;
const int FontWidthPx = 10;
const int FontHeightPx = 20;
void PrintStringCenter(char* s)
{
int x = (ScreenWidthPx-strlen(s)*FontWidthPx)/2;
int y = (ScreenHeightPx-FontHeightPx)/2;
g15r_renderString(canvas, s, 0, G15_TEXT_MED, x, y);
} |
|
|
Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jul 14, 2009 12:35 pm Post subject: |
|
|
Jani:
I made this
Code: | void WriteTextCenter(unsigned char stringOut[], int y)
{
const int ScreenWidth = 160; //The width is 160, not 80
int c = (ScreenWidth - strlen(stringOut)) / 2;
g15r_renderString(canvas, stringOut, 0, G15_TEXT_MED, c, y);
} |
Unfortunatly, it did not work as intended. The text started at 80 or something around that.
I think the problem might be because of stringOut[].
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 14, 2009 12:37 pm Post subject: |
|
|
I tested my first code with Windows console and it works fine. So, are you messing with pixels or characters when talking about coordinates?
I know nothing about G15, but I do know something about general console programming :)
|
|
Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jul 14, 2009 12:40 pm Post subject: |
|
|
Jani wrote: | I tested my first code with Windows console and it works fine. So, are you messing with pixels or characters when talking about coordinates?
I know nothing about G15, but I do know something about general console programming  |
I'm talking about pixels, which is why I don't think strlen() is the best thing to use, as it only counts the characters in a string, not pixels. (That's correct, aye?)
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 14, 2009 12:41 pm Post subject: |
|
|
GNU wrote: | I'm talking about pixels, which is why I don't think strlen() is the best thing to use, as it only counts the characters in a string, not pixels. (That's correct, aye?) | Yeah, but just multiply it by the font width and there we go. ..as long as you're using a monospaced font (which I believe you're using). Check my other example.
|
|
Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Tue Jul 14, 2009 12:53 pm Post subject: |
|
|
GetTextExtentPoint or GetTextExtentPoint32 to get the width of the string in pixels.
start position =([width of screen] - [width of the string in pixels])/2.
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 14, 2009 12:57 pm Post subject: |
|
|
HolyBlah wrote: | GetTextExtentPoint or GetTextExtentPoint32 to get the width of the string in pixels.
start position =([width of screen] - [width of the string in pixels])/2. | The only but is that we're talking about programming an embedded device, so I doubt WINAPI is available.. But again I don't know anything about G15.
EDIT: This topic made me to look for small LCD screens I could mod into my PC or just have lying on my desk :) I've been using ATmega128 board and it has some leds and an LCD and it's so much fun to play with. But it's just too ugly to be on the desk for show all the time. I think I'll get an LCD off somewhere this week
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Jul 14, 2009 1:06 pm Post subject: |
|
|
HolyBlah wrote: | GetTextExtentPoint or GetTextExtentPoint32 to get the width of the string in pixels.
start position =([width of screen] - [width of the string in pixels])/2. |
nifty.. those were the APIs i should've wanted back in the day lol..
@ jani, those APIs work for windows embedded CE
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 14, 2009 1:07 pm Post subject: |
|
|
Slugsnack wrote: | @ jani, those APIs work for windows embedded CE | Has Logitech Windows CE? I doubt it... It's just an embedded LCD screen with their own SDK. But oh well, I might have made a mistake calling it WINAPI, but anyway, you got my point. I wonder if Windows Embedded API is called WINAPI, but I think it isn't..
Sleep time, nights.
|
|
Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jul 14, 2009 1:51 pm Post subject: |
|
|
I'm afraid WinAPI is kinda useless for me, as I'm a Linux user.
|
|
Back to top |
|
 |
|