Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Text input using allegro 5

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Text input using allegro 5
Neil Roy
Member #2,229
April 2002
avatar

Well, if you look over my code you can see that I handled it with no problem, and that was one of the first times I had really dealt with Unicode. If you want to make a game these days and want to make certain anyone, from any language can enter their name, you want unicode. Personally, I didn't have a problem with it and have never had a problem handling C strings, or pointers etc.

This is the main reason I posted the code I use in my game. It's simple and works well. I think this is one area where Allegro done a great job. I don't find it difficult at all, look at my code, that's pretty simple, part of that code has stuff in it for my sound effects and what not, trim them out and there isn't much to it.

In any event, whether you find it more difficult or not, if you wish to create a game that anyone can use, you will have to support this or limit your audience. And the vast majority or people that play my game are people from other countries, usually non-English speaking nations. For fun or for profit, there's a huge market out there of people in non-English speaking countries. It's best to learn to do it right early on.

Scooter
Member #16,799
January 2018

Good afternoon to Neil and Edgar:
I am so sorry that I caused you two to have bad feelings between you. That
certainly was NOT my intention. Hope you can mend your differences. I only ask
a question because I need help and I still do. I have been working on this problem
for the past two days and have gotten nowhere. To me, there is no reason why this should be so hard to figure out. I have been able to do everything I needed to do
with allegro 5, but this keyboard is a problem I can't solve. The code supplied
allows me to press a key and it will show on the screen, but it is beyond me how
to get it in my buffer and be able to display it. I have tried al_draw_ustr() and
everything else I could fine but nothing is displayed. I am still shooting in the
dark on this one. Oh, I forgot to mention I have used al_ustr_to_buffer() but still
nothing. I will get back in touch with you later. Hang on, its been a bumpy ride so far. Have a great day my friends!

Edgar Reynaldo
Member #8,592
May 2007
avatar

Scooter said:

Good afternoon to Neil and Edgar:
I am so sorry that I caused you two to have bad feelings between you. That
certainly was NOT my intention. Hope you can mend your differences.

Don't worry about it Scooter. Neil and I argue all the time. It's normal. Yes, we're a little dysfunctional around here...

Scooter said:

it is beyond me how to get it in my buffer and be able to display it.

You need to use a string concatenation function, whether it be for a c-string (whether it's encoded in ascii, utf-8 or something else) or an ALLEGRO_USTR, or whatever you want to store it in. Then you need to draw it on the screen using allegro and an ALLEGRO_FONT* that you create.

Show us the code that you're using, and we can tell you what needs to be fixed.

Use <code>code_goes_here();</code> tags to post code.

I'll try to come up with a quick example in the mean time.

Neil Roy said:

1 Timothy 5:1 (NIV)
Do not rebuke an older man harshly, but exhort him as if he were your father.

Ephesians 6:4 said:

4 Fathers, do not provoke your children to anger; instead, bring them up in the discipline and instruction of the Lord.

8-)

Chris Katko
Member #1,881
January 2002
avatar

Scooter said:

I am so sorry that I caused you two to have bad feelings between you.

They're both adults. If they're in an argument, it's nobody's fault but their own. :)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Neil Roy
Member #2,229
April 2002
avatar

If you scroll down through the messages, you will see that I didn't start any argument. But I won't be helping anyone in these forums again, this way "Edgar" won't get upset.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil, I never had any problem with you helping someone. But complicating things needlessly for beginners is a bad idea, and it is not what he asked for! You're the one who started insulting me.

Neil Roy said:

But don't ing criticize MY Unicode because it doesn't have an upper limit, it only shows off your ignorance. >:( ::)

So I'm ignorant. Thanks.

Neil Roy said:

But feel free to piss around with other methods.

So my methods aren't worth using? That's funny. The first thing I posted was working example code, and then I posted the allegro example program.

I find it odd that it's always my fault when you're the one who is continually getting upset at me because I don't agree with you. Don't have a hissy fit just because you're wrong. Grow the fuck up and deal with it.

Neil Roy
Member #2,229
April 2002
avatar

You want my advise Scooter? Honestly I advise you stay far away from Allegro, any version. A MUCH better API is SDL2.

It's better supported, has a few AAA titles under it's belt, pre-compiled versions of it are super easy to get, and they work as intended.

And you can get plenty of help with it online without someone constantly engaging in arguments. There are tons of Youtube videos with help, the SDL2 wiki is amazing, probably the best designed for help I have seen.

It's what I have switched to. I won't make anything with Allegro ever again. It's a dead API. It was good in it's day, but it's day has passed.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy said:

You want my advise Scooter? Honestly I advise you stay far away from Allegro, any version. A MUCH better API is SDL2.

It's better supported, has a few AAA titles under it's belt, pre-compiled versions of it are super easy to get, and they work as intended.

And you can get plenty of help with it online without someone constantly engaging in arguments. There are tons of Youtube videos with help, the SDL2 wiki is amazing, probably the best designed for help I have seen.

It's what I have switched to. I won't make anything with Allegro ever again. It's a dead API. It was good in it's day, but it's day has passed.

You're a traitor and a coward. Good riddance to bad rubbish.

@Scooter
Don't listen to him, he's just a bitter old man.

Allegro 5 has a very good API, and you can do it all easier with Allegro than you can with SDL2. We may not be as popular and we may not have as many users, but there are AAA titles made with Allegro 5 as well, such as Factorio and others. Our small user base makes it harder for us to distribute binaries, but I have been releasing binaries for MinGW for the last several years, as well as the binaries that SiegeLord provides on liballeg.org. Neil himself has been using my binaries for a while now. Or didn't he tell you that? I'm sure he's going to boycott me now, as well as allegro, because he can't stand people disagreeing with him. He's just a sore loser.

And if our forum has problems with people arguing, it's usually limited to the off topic ordeals board. But Neil likes to start shit with me, and I don't care for it. I don't start fights, I finish them.

Our forums are usually very good natured, and people almost always get help within a day or two. If that's too long, you can go on IRC on #allegro on freenode.net and get help almost right away.

Now I'll try to get back to making an example for you like I said.

If your code isn't working, post it. Then we can show you what's wrong.

Neil Roy
Member #2,229
April 2002
avatar

Just being honest and giving him alternative choices. It's always good to try different things and see which one works best for you.

Scooter
Member #16,799
January 2018

Good afternoon Edgar:
I finally have something that works pretty good. If you have time can you look
this over. You would not believe the hours I have in this code! If you have the
time, I would love to see this in strictly ascii code. I do not want to see the
words unichar or ustr ever again! This represents the worse problem I have ever
had with Allegro. Here's the code:

Edgar Reynaldo
Member #8,592
May 2007
avatar

It's a little late tonight, but I'll give you feedback on your code tomorrow if no one else does. I've also put together a full example program but I have to work out a couple kinks first. Too tired to work on it anymore tonight though.

EDIT

Here's an example program and the accompanying source code. It's written entirely in C (for your sake) and uses an ALLEGRO_USTR and an unsigned int code point array (because I hate variable width characters).

InputDemo.zip

{"name":"611284","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/6\/16c0fb481952a8a3024a7a2550c4725f.png","w":802,"h":633,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/6\/16c0fb481952a8a3024a7a2550c4725f"}611284

Regarding your source code, there are a few things you could improve. It's pretty disorganized. The formatting is rough and inconsistent. Combined, those two things make it hard to read and understand. It's a decent start, but could use some organization and proper formatting.

In my source code I tried to comment as well as I could so you can understand what I'm doing. If you have any questions ask away.

My example program is pretty functional, but I didn't include everything that a real text box input widget would have.

Personally, I would much rather avoid C altogether in favor of C++. Manually inserting and deleting characters into an array is kind of obnoxious in C. The string is stored in an unsigned integer array, and then encoded into an ALLEGRO_USTR for display on demand.

Here's the code :

#SelectExpand
1#include "allegro5/allegro.h" 2#include "allegro5/allegro_font.h" 3#include "allegro5/allegro_ttf.h" 4#include "allegro5/allegro_primitives.h" 5 6#include <stdio.h> 7 8 9void AssertHandler(const char* expr , const char* file , int line , const char* func) { 10 int i = 0; 11 12 printf("%s failed in file %s on line %d in function %s\n" , expr , file , line , func); 13 14 i /= 0; 15 return; 16} 17 18 19 20void CreateUstrFromCodePoints(unsigned int* cbuf , int size , ALLEGRO_USTR** pstr) { 21 if (!cbuf) {return;} 22 if (size < 0) {return;} 23 if (!pstr) {return;} 24 ALLEGRO_USTR* ustr = *pstr; 25 if (ustr) {al_ustr_free(ustr);} 26 *pstr = al_ustr_new(""); 27 ustr = *pstr; 28 for (int i = 0 ; i < size ; ++i) { 29 al_ustr_append_chr(ustr , cbuf[i]); 30 } 31 return; 32} 33 34 35 36int main(int argc , char** argv) { 37 38 /// Declarations 39 40 int ww = 800; 41 int wh = 600; 42 43 bool redraw = true; 44 bool quit = false; 45 46 bool caret_blink = false; 47 float caret_duration = 0.5f; 48 float elapsed_time = 0.0f; 49 50 /// For holding the current string. Just allocate more than we'll need instead of messing around with dynamic arrays 51 const int MAXNCHARS = 24; 52 unsigned int intcharbuf[MAXNCHARS + 1]; 53 ALLEGRO_USTR* ustr = al_ustr_new(""); 54 55 int caret = 0; 56 int nchars = 0; 57 58 /// For holding stored strings 59 const int MAXNLINES = 30; 60 ALLEGRO_USTR* ustrs[MAXNLINES]; 61 int nlines = 0; 62 63 bool insert_mode = true; 64 65 ALLEGRO_DISPLAY* d = 0; 66 ALLEGRO_EVENT_QUEUE* q = 0; 67 ALLEGRO_TIMER* t = 0; 68 ALLEGRO_FONT* f = 0; 69 70 const int tx = ww/2; 71 const int ty = 64; 72 73 /// Code 74 75 (void)argc; 76 (void)argv; 77 78 memset(intcharbuf , 0 , (MAXNCHARS + 1)*sizeof(unsigned int)); 79 memset(ustrs , 0 , MAXNLINES*sizeof(ALLEGRO_USTR*)); 80 81 if (!al_init()) {return 1;} 82 if (!al_init_font_addon()) {return 2;} 83 if (!al_init_ttf_addon()) {return 3;} 84 if (!al_init_primitives_addon()) {return 4;} 85 86 if (!al_install_keyboard()) {return 10;} 87 if (!al_install_mouse()) {return 11;} 88 89 al_register_assert_handler(AssertHandler); 90 91 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED); 92 93 d = al_create_display(ww,wh); 94 q = al_create_event_queue(); 95 t = al_create_timer(1.0 / 60.0); 96 f = al_load_ttf_font("Consola.ttf" , -24 , 0); 97 98 if (!d || !q || !t || !f) {return -1;} 99 100 101 al_register_event_source(q , al_get_keyboard_event_source()); 102 al_register_event_source(q , al_get_mouse_event_source()); 103 al_register_event_source(q , al_get_display_event_source(d)); 104 al_register_event_source(q , al_get_timer_event_source(t)); 105 106 al_start_timer(t); 107 108 while(!quit) 109 { 110 if(redraw) 111 { 112 al_clear_to_color(al_map_rgb(55,111,222)); 113 114 /// Draw text label 115 al_draw_text(f , al_map_rgb(0,0,64) , tx , ty - al_get_font_line_height(f) - 5 , ALLEGRO_ALIGN_LEFT , "Enter text here :"); 116 117 /// Draw our text box and text 118 { 119 int tbx,tby,tbw,tbh; 120 al_get_ustr_dimensions(f , ustr , &tbx , &tby , &tbw , &tbh); 121 int x = tx + tbx; 122 int y = ty + tby; 123 al_draw_filled_rectangle(x , y , x + tbw , y + tbh , al_map_rgb(0,0,0)); 124 al_draw_rectangle(x - 5 , y - 5 , x + tbw + 5 , y + tbh + 5 , al_map_rgb(255,255,255) , 5.0f); 125 } 126 al_draw_ustr(f , al_map_rgb(255,255,255) , tx , ty , ALLEGRO_ALIGN_LEFT , ustr); 127 128 /// Draw our caret 129 if (!caret_blink) { 130 ALLEGRO_USTR* sub = al_ustr_dup_substr(ustr , 0 , al_ustr_offset(ustr , caret)); 131 132 int cx = al_get_ustr_width(f , sub); 133 134 al_draw_text(f , al_map_rgb(0,255,0) , tx + cx - al_get_text_width(f , "|")/2 , ty , 0 , "|"); 135 136 al_ustr_free(sub); 137 } 138 139 /// Draw our stored strings 140 { 141 int lh = 1.5*al_get_font_ascent(f); 142 143 /// Column 1 lower left 144 int y = wh - (MAXNLINES/2 + 1)*lh; 145 int x = ww/3; 146 int align = ALLEGRO_ALIGN_CENTER; 147 for (int i = 0 ; i < MAXNLINES/2 && i < nlines ; ++i) { 148 al_draw_ustr(f , al_map_rgb(0,255,255) , x , y , align , ustrs[i]); 149 y += lh; 150 } 151 152 /// Column 2 lower right 153 x = 2*ww/3; 154 y = wh - (MAXNLINES/2 + 1)*lh; 155 for (int i = MAXNLINES/2 ; i < nlines ; ++i) { 156 al_draw_ustr(f , al_map_rgb(0,255,255) , x , y , align , ustrs[i]); 157 y += lh; 158 } 159 } 160 /// Draw some debug info 161 al_draw_textf(f , al_map_rgb(255,127,0) , 10 , 10 , ALLEGRO_ALIGN_LEFT , "MAXNCHARS = %d" , MAXNCHARS); 162 al_draw_textf(f , al_map_rgb(255,127,0) , 10 , 30 , ALLEGRO_ALIGN_LEFT , "nchars = %d" , nchars); 163 al_draw_textf(f , al_map_rgb(255,127,0) , 10 , 50 , ALLEGRO_ALIGN_LEFT , "nlines = %d" , nlines); 164 al_draw_textf(f , al_map_rgb(255,127,0) , 10 , 70 , ALLEGRO_ALIGN_LEFT , "caret = %d" , caret); 165 al_draw_textf(f , al_map_rgb(255,127,0) , 10 , 90 , ALLEGRO_ALIGN_LEFT , "Insert = %s" , (insert_mode?"On":"Off")); 166 167 al_flip_display(); 168 redraw = false; 169 } 170 do 171 { 172 ALLEGRO_EVENT ev; 173 al_wait_for_event(q, &ev); 174 175 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 176 quit = true; 177 } 178 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 179 quit = true; 180 } 181 if(ev.type == ALLEGRO_EVENT_TIMER) 182 { 183 elapsed_time += al_get_timer_speed(t); 184 if (elapsed_time >= caret_duration) { 185 caret_blink = !caret_blink; 186 elapsed_time = 0; 187 } 188 redraw = true; 189 } 190 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button == 1) { 191 /// Find new caret position if mouse is over string 192 int uw = al_get_ustr_width(f , ustr); 193 int mx = ev.mouse.x; 194 int y = ev.mouse.y - ty; 195 if (y >= 0 && y <= al_get_font_line_height(f)) { 196 int x = mx - tx; 197 if (x >= 0 && x <= uw) { 198 float cw = (float)uw/nchars; 199 caret = (int)(x / cw); 200 caret_blink = false; 201 elapsed_time = 0.0f; 202 } 203 } 204 } 205 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 206 if (ev.keyboard.keycode == ALLEGRO_KEY_INSERT) { 207 insert_mode = !insert_mode; 208 } 209 } 210 if (ev.type == ALLEGRO_EVENT_KEY_CHAR) { 211 212 if (ev.keyboard.keycode == ALLEGRO_KEY_ENTER) { 213 /// Add the current string to our set of strings and clear 214 CreateUstrFromCodePoints(intcharbuf , nchars , &ustr); 215 216 /// Insert the string in the front of the array 217 if (nlines == MAXNLINES) { 218 al_ustr_free(ustrs[MAXNLINES - 1]);/// So we don't leak memory 219 } 220 for (int i = nlines ; i >= 1 ; --i) { 221 ustrs[i] = ustrs[i - 1]; 222 } 223 ustrs[0] = ustr; 224 nlines++; 225 if (nlines > MAXNLINES) { 226 nlines = MAXNLINES; 227 } 228 ustr = al_ustr_new(""); 229 memset(intcharbuf , 0 , sizeof(unsigned int)*(MAXNCHARS + 1)); 230 nchars = 0; 231 caret = 0; 232 } 233 else if (ev.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) { 234 /// Delete character before caret position 235 if (caret >= 1) { 236 for (int crt = caret - 1 ; crt < MAXNCHARS ; ++crt) { 237 intcharbuf[crt] = intcharbuf[crt + 1]; 238 } 239 --nchars; 240 if (nchars == 0) { 241 memset(intcharbuf , 0 , MAXNCHARS*sizeof(unsigned int)); 242 } 243 --caret; 244 } 245 } 246 else if (ev.keyboard.keycode == ALLEGRO_KEY_DELETE) { 247 if (caret < nchars) { 248 /// Delete character at the caret position 249 for (int crt = caret ; crt < nchars ; ++crt) { 250 intcharbuf[crt] = intcharbuf[crt + 1]; 251 } 252 --nchars; 253 if (nchars == 0) { 254 memset(intcharbuf , 0 , MAXNCHARS*sizeof(unsigned int)); 255 } 256 if (nchars < 0) { 257 nchars = 0; 258 } 259 } 260 } 261 else if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) { 262 /// Move caret left 263 caret--; 264 if (caret < 0) {caret = 0;} 265 caret_blink = false; 266 elapsed_time = 0.0f; 267 } 268 else if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) { 269 /// Move caret right 270 caret++; 271 if (caret >= nchars) { 272 caret = nchars; 273 } 274 caret_blink = false; 275 elapsed_time = 0.0f; 276 } 277 else { 278 /// We have a character, add it to our buffer 279 unsigned int uc = ev.keyboard.unichar; 280 281 printf("%X typed\n" , uc); 282 283 if (uc >= 32) { 284 if (insert_mode) { 285 /// Move all chars right from the caret on 286 for (int crt = MAXNCHARS - 1 ; crt >= caret ; --crt) { 287 intcharbuf[crt] = intcharbuf[crt - 1]; 288 } 289 /// We're inserting a new character, so bump the char count 290 nchars += 1; 291 if (nchars > MAXNCHARS) { 292 nchars = MAXNCHARS; 293 } 294 } 295 else { 296 /// Overwrite mode 297 if (caret == nchars && nchars < MAXNCHARS) { 298 nchars++;/// We're on the right end of the string, increase the char count 299 } 300 } 301 /// Add our new character 302 intcharbuf[caret++] = uc; 303 if (caret >= MAXNCHARS) { 304 caret = MAXNCHARS; 305 } 306 } 307 } 308 309 /// Refresh our USTR 310 CreateUstrFromCodePoints(intcharbuf , nchars , &ustr); 311 312 } 313 } 314 while(!al_is_event_queue_empty(q)); 315 316 } 317 318 for (int i = 0 ; i < nlines ; ++i) { 319 al_ustr_free(ustrs[i]); 320 ustrs[i] = 0; 321 } 322 nlines = 0; 323 al_ustr_free(ustr); 324 ustr = 0; 325 nchars = 0; 326 327 al_uninstall_system(); 328 329 return 0; 330}

Scooter
Member #16,799
January 2018

Good morning Edgar:
I have downloaded you program and I say WOW! That is a lot of code to
digest! That will keep me busy for quite a while. I switch to Linux a few
years back, but I do have some Windows machines around here. I will fire
one up and give your program a test. I think its great that you have this
stuff figured out. I will get back to you later. Have a great day my friend!

Edgar Reynaldo
Member #8,592
May 2007
avatar

Scooter
Member #16,799
January 2018

Good afternoon Edgar:
I am trying to compile your program on Linux with Code:Blocks. It is
generating some errors. This is one thing its complaining about on line 33.

for (int i = 0 ; i < size ; ++i) {
al_ustr_append_chr(ustr , cbuf[i]);

Code:Blocks returns this error:

error:'for' loop initial declarations are only allowed in C99 mode. I find nothing on Code:Blocks that gives me a clue what this means and what to do about it.

There are other errors being reported but most are concerned with the 'for loops'.

I use 'for loops' all the time, but never seen this error. New one on me!

Edgar Reynaldo
Member #8,592
May 2007
avatar

Scooter, here's the fix for that :

#SelectExpand
1void CreateUstrFromCodePoints(unsigned int* cbuf , int size , ALLEGRO_USTR** pstr) { 2 3 int i = 0;/// Move declaration of i here 4 5 if (!cbuf) {return;} 6 if (size < 0) {return;} 7 if (!pstr) {return;} 8 ALLEGRO_USTR* ustr = *pstr; 9 if (ustr) {al_ustr_free(ustr);} 10 *pstr = al_ustr_new(""); 11 ustr = *pstr; 12 for (i = 0 ; i < size ; ++i) {/// Remove int declaration here 13 al_ustr_append_chr(ustr , cbuf[i]); 14 } 15 return; 16}

C is whiny about stuff sometimes. You can't declare new variables after you write code. The declarations always have to go at the top of the function, or into a new code block like so :

#SelectExpand
1#include <stdio.h> 2 3int main(int argc , char** argv) { 4 5 int a = 0; 6 7 printf("%d" , a); 8 9 int b = 1;/// Illegal, because it comes after the printf statement 10 11 { 12 int c = 2;/// perfectly legal, because it is at the top of the new scope 13 /// defined by the braces {} 14 printf("%d" , c); 15 } 16 return 0; 17}

Audric
Member #907
January 2001

Even single-line comments are not C :) They are introduced by either :
- C++ (every version and implementation, it's part of the base standard)
- ISO C99
- GNU extension (any gcc compiler where you don't disable them by specifying a compilation standard that excludes them)
- Other compiler-specific extensions ? (MSVC6 maybe?)

jmasterx
Member #11,410
October 2009

In 2018, unless you're creating a really specialized system or you care about ridiculous portability , or writing a library, I do not really get the appeal of using a C compiler to write C.

C++ adds those few things that just makes life easier. You can still program C-style in C++ but without having to worry about silly things like line comments.

I would also make a few thin wrappers around basic stl things like vector, map, etc, rather than reinventing the wheel for such basic things.

Not really relevant to a C question, but I have a class in Agui for utf8 manipulations on std strings https://github.com/jmasterx/Agui/blob/master/include/Agui/UTF8.hpp

Neil Roy
Member #2,229
April 2002
avatar

Taking advice from people who say things like this...

jmasterx said:

You can still program C-style in C++ but without having to worry about silly things like line comments.

Is about as foolish as one can get. Comments?! I have coded in C since the '90s and haven't "worried about comments" ever. Single line has been a part of C for two decades. Just about everything in C++ comes from C, that's why it has a "C" in it's name.

Here's a fun thought, if you pulled everything that is C out of C++ you would be left with an unusable language... basic things like if/else, switch/case, {}, return, headers, includes... you name it, it is all C. The ++ is things like classes and such that is C++ specific... but comments... yeah... comments, lets focus on that. Stupidity.

You can't declare new variables after you write code.

This is simply laughable and blatantly untrue! I have been declaring variables mid code for two decades now! It hasn't been a problem since the '90s, and even then, it wasn't a problem.

Lets just test out the code mentioned with GCC and see what happens shall we? I'll compile it with just "gcc main.c" and I will post a screenshot of all warnings and errors to be fair and lets just see if what was stated is true or not...

#SelectExpand
1#include <stdio.h> 2 3int main(int argc , char** argv) { 4 5 int a = 0; 6 7 printf("%d" , a); 8 9 int b = 1;/// Illegal, because it comes after the printf statement 10 11 { 12 int c = 2;/// perfectly legal, because it is at the top of the new scope 13 /// defined by the braces {} 14 printf("%d" , c); 15 } 16 return 0; 17}

I typed "gcc -v" to show the version, ironically, this is the mingw version I got from Edgar. I then just typed "gcc main.c" with the above code, unaltered, even the /// in there... this was the result...

{"name":"611287","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/ce74302a99d6078ae445f24cef04d309.jpg","w":775,"h":173,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/ce74302a99d6078ae445f24cef04d309"}611287

Notice, I didn't tell it which standard of C I was using, I used the code above EXACTLY as Edgar posted it with zero errors and not even a warning.

Now these arguments would have been true in the mid 1990s! But haven't been true for a very long time. And of course now there is the 2011 C standard which adds in more functionality. Heck, C++ itself has changed dramatically since the same time, and for the better. Comparing modern C++ to old C? How is that even remotely fair?

Now if you want to use C++, go for it. I can certainly see the appeal, but at least be honest and compare it to MODERN C and use the one you want because YOU like it. C isn't going anywhere, anytime soon.

There's good reason why the majority of libraries and drivers are written in C.

Use what you feel more comfortable with, but do your own research on this and pull out a modern C compiler like GCC and test what they said and see if it is true or a lie.

Edit: the following alterations to the above code also compiles just fine with "gcc main.c"

#SelectExpand
1#include <stdio.h> 2#include <stdbool.h> 3 4int main(int argc , char** argv) { 5 6 int a = 0; 7 8 printf("%d" , a); 9 10 int b = 1;/// Illegal, because it comes after the printf statement 11 bool x = true; 12 13 { 14 int c = 2;/// perfectly legal, because it is at the top of the new scope 15 /// defined by the braces {} 16 printf("%d" , c); 17 } 18 return 0; 19}

jmasterx
Member #11,410
October 2009

You took what I said a bit out of context. My point was just, yeah, C++ is C with the ++.
Which means you can still do everything C can do with the ++. You conveniently ignored my portion about stl.

The main advantage I see of using pure modern C, is not depending on the C++ runtime.

and for things like hardware drivers that makes sense.

But trying to program a decent video game without some equivalent of stl (or boost & co) is silly. You either roll it out by hand or find some library that you have to learn and work around its license and quirks. STL is tried, tested and heavily supported on sites like SO, so it's great for people learning to code who do not yet understand how to make dynamic arrays and the such.

Neil, you do honestly come across like you're bashing with that kind of talk. It's hard to defend that. You're treating us like we're children or something and that's very unwelcome.

You could have stated your point without making me seem like an uncredible piece of s​hit. >:(

Edgar Reynaldo
Member #8,592
May 2007
avatar

Guess what, people still use compilers that only support C90, and that code in C90 is illegal. My compiler happens to be more recent, and uses a different default standard. Instead of being a C Nazi, why don't you chill out and take a step back and realize that not everyone is using a modern compiler. Granted, they should upgrade or compile with a different standard. But all this hate towards C++ is just immature and ridiculous. Grow up.

Polybios
Member #12,293
October 2010

Our most Christian Christians acting out their agape again... So sad. :'(

Neil Roy
Member #2,229
April 2002
avatar

Guess what, those same people will also still be using compilers that do not support modern C++ as well, so the same comment can be made about that! The fact is, modern compilers which are freely available, which you yourself uses has NO problems with the bullshit code you posted! Making you our to be a liar!

And I NEVER stated I hate C++, I am defending the blatent hatred and falsehoods being posted about C!

THIS is what I said about C++

NiteHackr said:

Now if you want to use C++, go for it. I can certainly see the appeal

Just don't post code and state that C will reject it when it will not. When you discuss these things, you need to compare equal versions. So if you want to compare 1990 C++ with 1990 C, go for it. But it is unfair to compare 2018 C++ with 1990 C!

If you prefer C++, great, wonderful, it's certainly a very powerful language. There are aspects I like about it for sure. It does strings really well, it has vectors which are awesome. But be honest about it all. Compare modern C with modern C++. And modern C can do threads, one line comments, variable declarations mid code etc... etc... etc... it just doesn't have classes, which is something I can't stand and I have even seen a move by some away from them as the problems with classes are starting to become apparent, but that's another topic. I'm not posting to attack C++, I am only defending the lies being posted against C.

Polybios said:

Our most Christian Christians acting out their agape again... So sad. :'(

What the fuck does this have to do with religion moron?! This is about programming, grow a brain!

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

Than it's clear he needs to download one! Who the hell doesn't have a modern compiler these days?! It's not like they're hard to find. In either way, your information about C was completely false.

Edgar Reynaldo
Member #8,592
May 2007
avatar

 1   2   3 


Go to: