Allegro.cc - Online Community

Allegro.cc Forums » Allegro.cc Comments » updates to <code> tag

This thread is locked; no one can reply to it. rss feed Print
 1   2 
updates to <code> tag
Matthew Leverton
Supreme Loser
January 1999
avatar

The code tag now looks like:

<code name="filename" start="1">
</code>

where both parameters are optional. The name attribute is meant to be the name or title of the source code. The start attribute is the first line number. If omitted, it is assumed to be 1.

Short code snippets with no name and starting line number will be inline without any chrome.

Lines can be highlighted by appending *** to them.

To turn off line numbers (copy / paste) click the # button. Click the 'E' to get rid of the scrollbars.

allegro.c#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4 5#include "allegro5/allegro5.h" 6#include "allegro5/internal/aintern.h" 7#include "allegro5/internal/aintern_dtor.h" 8#include "allegro5/internal/aintern_memory.h" 9 10 11 12#define PREFIX_I "al-main INFO: " 13#define PREFIX_W "al-main WARNING: " 14#define PREFIX_E "al-main ERROR: " 15 16 17/* debugging stuff */
18static int debug_assert_virgin = true;
19static int debug_trace_virgin = true; 20 21static FILE *assert_file = NULL; 22static FILE *trace_file = NULL; 23 24static int (*assert_handler)(AL_CONST char *msg) = NULL; 25int (*_al_trace_handler)(AL_CONST char *msg) = NULL; 26 27 28/* dynamic registration system for cleanup code */ 29struct al_exit_func { 30 void (*funcptr)(void); 31 AL_CONST char *desc; 32 struct al_exit_func *next; 33}; 34 35static struct al_exit_func *exit_func_list = NULL; 36 37 38 39/* _al_add_exit_func: 40 * Adds a function to the list that need to be called on Allegro shutdown. 41 * `desc' should point to a statically allocated string to help with 42 * debugging. 43 */ 44void _al_add_exit_func(void (*func)(void), AL_CONST char *desc) 45{ 46 struct al_exit_func *n; 47 48 for (n = exit_func_list; n; n = n->next) 49 if (n->funcptr == func) 50 return; 51 52 n = _AL_MALLOC(sizeof(struct al_exit_func)); 53 if (!n) 54 return; 55 56 n->next = exit_func_list; 57 n->funcptr = func; 58 n->desc = desc; 59 exit_func_list = n; 60} 61 62 63 64/* _al_remove_exit_func: 65 * Removes a function from the list that need to be called on Allegro 66 * shutdown. 67 */ 68void _al_remove_exit_func(void (*func)(void)) 69{ 70 struct al_exit_func *iter = exit_func_list, *prev = NULL; 71 72 while (iter) { 73 if (iter->funcptr == func) { 74 if (prev) 75 prev->next = iter->next; 76 else 77 exit_func_list = iter->next; 78 _AL_FREE(iter); 79 return; 80 } 81 prev = iter; 82 iter = iter->next; 83 } 84} 85 86 87 88/* _al_run_exit_funcs: 89 * Run all the functions registered with _al_add_exit_func, in reverse order of 90 * registration. 91 */ 92void _al_run_exit_funcs(void) 93{ 94 while (exit_func_list) { 95 void (*func)(void) = exit_func_list->funcptr; 96 _al_remove_exit_func(func); 97 (*func)(); 98 } 99} 100 101 102 103/* debug_exit: 104 * Closes the debugging output files. 105 */ 106static void debug_exit(void) 107{ 108 if (assert_file) { 109 fclose(assert_file); 110 assert_file = NULL; 111 } 112 113 if (trace_file) { 114 fclose(trace_file); 115 trace_file = NULL; 116 } 117 118 debug_assert_virgin = true; 119 debug_trace_virgin = true; 120} 121 122 123 124/* al_assert: 125 * Raises an assert (uses ASCII strings). 126 */ 127void al_assert(AL_CONST char *file, int line) 128{ 129 static int asserted = false; 130 int olderr = errno; 131 char buf[128]; 132 char *s; 133 134 if (asserted) 135 return; 136 137 /* todo, some day: use snprintf (C99) */ 138 sprintf(buf, "Assert failed at line %d of %s", line, file); 139 140 if (assert_handler) { 141 if (assert_handler(buf)) 142 return; 143 } 144 145 if (debug_assert_virgin) { 146 s = getenv("ALLEGRO_ASSERT"); 147 148 if (s) 149 assert_file = fopen(s, "w"); 150 else 151 assert_file = NULL; 152 153 if (debug_trace_virgin) 154 _al_add_exit_func(debug_exit, "debug_exit"); 155 156 debug_assert_virgin = false; 157 } 158 159 if (assert_file) { 160 fprintf(assert_file, "%s\n", buf); 161 fflush(assert_file); 162 } 163 else { 164 asserted = true; 165 166/* 167 if ((system_driver) && (system_driver->assert)) { 168 system_driver->assert(buf); 169 } 170 else { 171 */ 172// al_uninstall_system(); 173#ifndef ALLEGRO_MSVC 174 fprintf(stderr, "%s\n", buf); 175#endif 176 abort(); 177 // } 178 } 179 180 errno = olderr; 181} 182 183 184 185/* al_trace: 186 * Outputs a trace message (uses ASCII strings). 187 */ 188void al_trace(AL_CONST char *msg, ...) 189{ 190 int olderr = errno; 191 char buf[512]; 192 char *s; 193 194 /* todo, some day: use vsnprintf (C99) */ 195 va_list ap; 196 va_start(ap, msg); 197 vsprintf(buf, msg, ap); 198 va_end(ap); 199 200 if (_al_trace_handler) { 201 if (_al_trace_handler(buf)) 202 return; 203 } 204 205 if (debug_trace_virgin) { 206 s = getenv("ALLEGRO_TRACE"); 207 208 if (s) 209 trace_file = fopen(s, "w"); 210 else 211 trace_file = fopen("allegro.log", "w"); 212 213 if (debug_assert_virgin) 214 _al_add_exit_func(debug_exit, "debug_exit"); 215 216 debug_trace_virgin = false; 217 } 218 219 if (trace_file) { 220 fwrite(buf, sizeof(char), strlen(buf), trace_file); 221 fflush(trace_file); 222 } 223 224 errno = olderr; 225} 226 227 228 229/* register_assert_handler: 230 * Installs a user handler for assert failures. 231 */ 232void register_assert_handler(int (*handler)(AL_CONST char *msg)) 233{ 234 assert_handler = handler; 235} 236 237 238 239/* register_trace_handler: 240 * Installs a user handler for trace output. 241 */ 242void register_trace_handler(int (*handler)(AL_CONST char *msg)) 243{ 244 _al_trace_handler = handler; 245}

When making this post, I realized that there is a slight problem with multiline coloring (e.g., comments) that disables highlight ability and the original colors. That will be fixed later.

Refresh JS/CSS files.

Dustin Dettmer
Member #3,935
October 2003
avatar

Cool! I was gonna say it was ugly but then I refreshed the js/css. Very pretty.

cool.c#SelectExpand
125 int c = 46; 126
127 c = 43
128 129 if(c == 43)

The error is on line 127.

someone972
Member #7,719
August 2006
avatar

It takes a long time (two seconds roughly) to turn the line numbers off in IE7. Turning them back on is significantly faster.

A nice set of additions.

EDIT: Dustin's post removes the line #s fast.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Schyfis
Member #9,752
May 2008
avatar

Hiding and showing line numbers fast enough on Matthew's example breaks the scroll bar (Firefox 3.0.7).
Other than that, this is pretty sweet!

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

torhu
Member #2,727
September 2002
avatar

Cool stuff.

When I disable line numbers, the font size changes from 10 to 8. Using Firefox 3.1b3 on Windows.

Jonatan Hedborg
Member #4,886
July 2004
avatar

Very nice! I really like all the new work you've put into the site lately Matthew!
This is by far the best forum system I've used!

Schyfis said:

Hiding and showing line numbers fast enough on Matthew's example breaks the scroll bar

Same here

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

BAF
Member #2,981
December 2002
avatar

Now add support for multiple languages.

ReyBrujo
Moderator
January 2001
avatar

Very nice! Now it looks like a programming forum! Could you add some way of selecting all code instead of having to scroll around? That is the only thing I would want.

The only problem I find is that when scrolling with the well the page upwards or downwards, the top of the code box somehow jumps above the navigation bar of the site, so you get some strange effect.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Thomas Fjellstrom
Member #476
June 2000
avatar

Here's what it looks like in konqueror (4.3 svn), I'm sure you can see a couple flaws.

{"name":"accnewcodeboxkonqi.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/d\/2d9e97fe0d1c247dffc4b31cadee19bd.png","w":814,"h":425,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/d\/2d9e97fe0d1c247dffc4b31cadee19bd"}accnewcodeboxkonqi.png

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

BAF
Member #2,981
December 2002
avatar

Use a better browser.

Thomas Fjellstrom
Member #476
June 2000
avatar

edit:nm

edit2:
it was happening briefly with WebKit too, but now its not happening in either WebKit or KHTML.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

MiquelFire
Member #3,110
January 2003
avatar

I find it really odd that everything there's an update, my cache seems to have been cleared out enough that I don't need to hit Ctrl+F5...

test.c#SelectExpand
1int c; 2int y; 3 4int main(int n, char **args) {
5 int c;
6 return 0; 7}

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red | +Me
Windows 8 is a toned, stylish, polished professional athlete. But it’s wearing clown makeup, and that creates a serious image problem. ~PCWorld Article

bamccaig
Member #7,536
July 2006
avatar

main.cpp#SelectExpand
1#include <iostream> 2 3int main(int argc, char *argv[]) 4{
5 std::cout << "Allegro.cc FTW!\n" << std::endl;
6 7 return 0; 8}

Kibiz0r
Member #6,203
September 2005
avatar

The collapse/expand feature could stand to be a little more obvious. I don't think newbies would catch on without prompting.

Matthew Leverton
Supreme Loser
January 1999
avatar

It takes a long time (two seconds roughly) to turn the line numbers off in IE7. Turning them back on is significantly faster.

IE is a horrible web browser. If you have any choice, you shouldn't use it. Let's take a look at the benchmark of the said operation:

  1. Safari 4: 14 ms

  2. Opera 9.64: 50 ms

  3. FireFox 3: 61 ms

  4. IE 8: 817 ms

This is just a typical string build during a DOM traversal.

Safari is so fast you don't even realize anything has happened. Opera and Firefox are both nearly as fast—neither of them feel lagged. Then IE 8, Microsoft's latest and greatest browser, steps up to the plate and takes nearly a second.

But keep using IE.

ReyBrujo
Moderator
January 2001
avatar

Hmm... I would like the top (filename, numbering on/off) to be at the bottom too if you expand the code and scrolled to the bottom. Also, although I am not sure if it is possible at all, can I generate a URL to point to the line 104 of the code you posted at the top?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Matthew Leverton
Supreme Loser
January 1999
avatar

Fixed some bugs.

X-G
Member #856
December 2000
avatar

No way to do inline code snippets yet?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

torhu
Member #2,727
September 2002
avatar

Thanks for fixing the font size, but any chance of courier new 10 instead of 8? Maybe I'm just old, but reading text that small is not a joy.

Matthew Leverton
Supreme Loser
January 1999
avatar

You can use custom CSS to change things like that.

div.source-code div.inner td,
div.source-code div.inner pre {
  font-size: 10pt !important;
}

torhu
Member #2,727
September 2002
avatar

ReyBrujo
Moderator
January 2001
avatar

Neat, now it looks precious.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Darizel
Member #10,585
January 2009
avatar

ReyBrujo said:

precious

Looks at your avatar
My precioussss....

----------
struthersgames.com - fun games for free download ;)
I am not an idiot.*
*in my opinion.

Matthew Leverton
Supreme Loser
January 1999
avatar

X-G said:

No way to do inline code snippets yet?

Just <code> with no new lines. e.g., while (false) create_bitmap(640, 480)

Thomas Fjellstrom
Member #476
June 2000
avatar

test: int test(char bar);***

seems not to work :(

int test(char bar);

works...

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2 


Go to: