Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C] Centering text on G15 LCD?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Tue Jul 14, 2009 10:31 am    Post subject: [C] Centering text on G15 LCD? Reply with quote

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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Jul 14, 2009 11:54 am    Post subject: Reply with quote

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
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Tue Jul 14, 2009 11:58 am    Post subject: Reply with quote

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
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Tue Jul 14, 2009 12:02 pm    Post subject: Reply with quote

; 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
View user's profile Send private message
Chaosis13
Master Cheater
Reputation: 0

Joined: 14 Aug 2007
Posts: 372

PostPosted: Tue Jul 14, 2009 12:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 14, 2009 12:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Tue Jul 14, 2009 12:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 14, 2009 12:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Tue Jul 14, 2009 12:40 pm    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 14, 2009 12:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
HolyBlah
Master Cheater
Reputation: 2

Joined: 24 Aug 2007
Posts: 446

PostPosted: Tue Jul 14, 2009 12:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 14, 2009 12:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Jul 14, 2009 1:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 14, 2009 1:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Tue Jul 14, 2009 1:51 pm    Post subject: Reply with quote

I'm afraid WinAPI is kinda useless for me, as I'm a Linux user.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites