Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 303 304 [305] 306 307 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 902601 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4560 on: June 26, 2013, 11:48:07 am »

EDIT2: This is annoying. Seems if you use [] to directly access a letter in a string, you get a signed string instead of an unsigned one, where numbers over 125 are turned negative. This was screwing up my text drawing pretty bad.
Code please. Are you saving an unsigned char to a signed one?
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4561 on: June 26, 2013, 12:17:17 pm »

EDIT2: This is annoying. Seems if you use [] to directly access a letter in a string, you get a signed string instead of an unsigned one, where numbers over 125 are turned negative. This was screwing up my text drawing pretty bad.
Code please. Are you saving an unsigned char to a signed one?
Nope.

Text[LetterCounter]%16*LetterWidth
Text[LetterCounter]/16*LetterHeight

Are the relevant bits of code. I am converting the letters into a location on a tileset. It was generating negative values when the letter was over 125. Fixed it by doing:

((unsigned char)Text[LetterCounter])%16*LetterWidth
((unsigned char)Text[LetterCounter])/16*LetterHeight
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4562 on: June 26, 2013, 02:25:20 pm »

EDIT2: This is annoying. Seems if you use [] to directly access a letter in a string, you get a signed string instead of an unsigned one, where numbers over 125 are turned negative. This was screwing up my text drawing pretty bad.
Code please. Are you saving an unsigned char to a signed one?
Nope.

Text[LetterCounter]%16*LetterWidth
Text[LetterCounter]/16*LetterHeight

Are the relevant bits of code. I am converting the letters into a location on a tileset. It was generating negative values when the letter was over 125. Fixed it by doing:

((unsigned char)Text[LetterCounter])%16*LetterWidth
((unsigned char)Text[LetterCounter])/16*LetterHeight

std::string::operator[] and std::string::at both return either a char& or const char&.  Assume everything in C/C++ is signed unless otherwise specified.

Why are you getting overflow above 125?  The maximum size of a signed char is 127.  Furthermore, how many tiles are you using?  Standard ASCII uses 128 characters indexed from 0..127.  Even if you have a lot of extra tiles, there are plenty of ascii indices that are for unprintable characters that you could be borrowing.

http://www.asciitable.com/

http://www.cplusplus.com/reference/string/string/operator[]/
http://www.cplusplus.com/reference/string/string/at/
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4563 on: June 26, 2013, 02:40:16 pm »

EDIT2: This is annoying. Seems if you use [] to directly access a letter in a string, you get a signed string instead of an unsigned one, where numbers over 125 are turned negative. This was screwing up my text drawing pretty bad.
Code please. Are you saving an unsigned char to a signed one?
Nope.

Text[LetterCounter]%16*LetterWidth
Text[LetterCounter]/16*LetterHeight

Are the relevant bits of code. I am converting the letters into a location on a tileset. It was generating negative values when the letter was over 125. Fixed it by doing:

((unsigned char)Text[LetterCounter])%16*LetterWidth
((unsigned char)Text[LetterCounter])/16*LetterHeight

std::string::operator[] and std::string::at both return either a char& or const char&.  Assume everything in C/C++ is signed unless otherwise specified.

Why are you getting overflow above 125?  The maximum size of a signed char is 127.  Furthermore, how many tiles are you using?  Standard ASCII uses 128 characters indexed from 0..127.  Even if you have a lot of extra tiles, there are plenty of ascii indices that are for unprintable characters that you could be borrowing.
Ah, my bad. I always keep messing those up. 127 is the right number.

I am using a few tiles. üõöä aren't in standard ASCII, and I have a bunch of control characters that I use for colours and stuff.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4564 on: June 26, 2013, 07:01:03 pm »

Whether default char is signed or unsigned in C++ is deliberately unspecified by the standard. If you ever care about it being one way or the other[1], always make that explicit. It really does vary from compiler to compiler, with GCC and MSVC even having compile-time flags to change the default behaviour of char.

[1] I'm gonna get ya, I'm gonna get ya get ya get ya
« Last Edit: June 26, 2013, 07:05:07 pm by MorleyDev »
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4565 on: June 26, 2013, 07:05:40 pm »

I had a related problem a while back.  I was trying to print a character to screen using an ascii bitmap font accessed by an array, but literals wouldn't work with the unsigned char parameter, and I didn't know what else to do to get them to work, so this is what I went with:

Code: [Select]
void Console::putChar(int x, int y, const char* c) {
    unsigned char c2 = *c;
    putChar(x, y, c2);
}
 
void Console::putChar(int x, int y, unsigned char c) { 
    int ascii = c;
    SDL_Rect letter = bmpFont->characters[ascii];
    // rest of character drawing stuff
}

Is there a better way to do this?  I'm very new to the C++ thing and I don't really know what I'm doing yet, all I know is that this made the Ñ appear on the screen.

Sorry, I missed the Preview button and posted this way too soon.
« Last Edit: June 26, 2013, 07:31:46 pm by SethCreiyd »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4566 on: June 26, 2013, 08:39:14 pm »

I had a related problem a while back.  I was trying to print a character to screen using an ascii bitmap font accessed by an array, but literals wouldn't work with the unsigned char parameter, and I didn't know what else to do to get them to work, so this is what I went with:

Code: [Select]
void Console::putChar(int x, int y, const char* c) {
    unsigned char c2 = *c;
    putChar(x, y, c2);
}
 
void Console::putChar(int x, int y, unsigned char c) { 
    int ascii = c;
    SDL_Rect letter = bmpFont->characters[ascii];
    // rest of character drawing stuff
}

Is there a better way to do this?  I'm very new to the C++ thing and I don't really know what I'm doing yet, all I know is that this made the Ñ appear on the screen.

Sorry, I missed the Preview button and posted this way too soon.
I think the top function isn't even necessary, since C++ does the numerical conversions automatically.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4567 on: June 26, 2013, 10:54:44 pm »

You could use int, no?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4568 on: June 27, 2013, 03:53:00 am »

So, I am in the process of self-teaching myself how to use Unity (and modeling with Blender. and texturing), and I manage to pry myself away from coding shinies and make what could technically count as a game (you can die and kill stuff). Also discovering how awesome Linq is.

Spoiler: important details (click to show/hide)
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

frostshotgg

  • Bay Watcher
  • It was a ruse
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4569 on: June 27, 2013, 05:36:01 am »

Guide to Blender: Press a hotkey, any hotkey. if you're lucky it does something.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4570 on: June 27, 2013, 05:41:08 am »

Guide to Blender: Press a hotkey, any hotkey. if you're lucky it does something.

No, if your lucky it won't do anything :P.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4571 on: June 27, 2013, 03:59:35 pm »

Blender is the Dwarf Fortress of 3D modelling software. Don't expect to learn it quickly, but when you do, you can get a lot done in a very short period of time. Compared to Maya, it's about the same amount of power, but with a focus on vertex manipulation, rather than face/shape manipulation. And having done both, when you get use to the keybindings for everything in Blender, switching to Maya's non-keybound menu based system feels like a chore.
« Last Edit: June 27, 2013, 04:02:03 pm by alway »
Logged

Toxicshadow

  • Bay Watcher
    • View Profile
    • github
Re: if self.isCoder(): post() #Programming Thread
« Reply #4572 on: June 27, 2013, 07:34:21 pm »

Nevermind, got it worked out.
« Last Edit: June 27, 2013, 08:53:55 pm by Toxicshadow »
Logged
Quote
'ere the Chias get hungry...

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4573 on: June 28, 2013, 12:30:00 am »

I think the top function isn't even necessary, since C++ does the numerical conversions automatically.

I commented it out and it works fine.  I guess I don't know what got it working, which probably isn't a good sign.  Thank you, though.

Now I'm confused by why you can pass a C string literal to the second function.  You can pass a string of any size and it will only print the first character of the array, which is exactly what I would have wanted to make it do if it weren't already doing it.

Is that something unique to strings, or something up to the compiler?  I tried to test an array of integers, but GCC gave a compile error:

Code: [Select]
    int n[10]; cout << int_to_string(n) << endl;

    // src/main.cpp:18:28: error: invalid conversion from 'int*' to 'int' [-fpermissive]
    // src/main.cpp:6:14: error:   initializing argument 1 of 'const char* int_to_string(int)' [-fpermissive]

I know fpermissive is a compiler flag, but I feel that messing with those is a bad idea until I know why one might.

You could use int, no?

It accepts integer literals, if that's what you mean, but numbers > 127 would still overflow.  If you mean using int as the parameter, I didn't think of it, or that it would affect anything.  My understanding of how types are converted is still pretty blurry. 
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4574 on: June 28, 2013, 05:57:49 am »

Arrays in c and c++ are actually pointers to the first member of the array, so that's why it printed the first character. You can pass them as regular pointers, but the things you pass them to won't know that it's an array.

The one exception is char* (AKA char pointers). They are considered arrays in most standard functions, and have to be null terminated, cause otherwise it goes outside the array and bad things happen. Generally a good idea to use c++ strings instead.

EDIT: The 2nd function takes the array? Hmmph.
EDIT2: Not over here it doesn't. "error: invalid conversion from 'char*' to 'unsigned char' [-fpermissive]" Are you sure you didn't pass it like putChar(x, y, *string)? Cause that would give the behaviour of taking the first letter.
« Last Edit: June 28, 2013, 06:10:24 am by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.
Pages: 1 ... 303 304 [305] 306 307 ... 796