YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

oder neudeutsch: Off-Topic
Antworten
theDon
Rätselmeister
Rätselmeister
Beiträge: 2219
Registriert: 11.06.2002, 18:22
Wohnort: gone (forever)
Kontaktdaten:

YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von theDon »

(c) 2002 by TheTinySteini, dp and theDon
TheTinySteini & dp - original cpp highlighting
theDon - ags highlighting & adaption to yabbse

so siehts aus:
//foo
int main(void)
{
return 23;
}
// --- Konstanten für Richtungen und Laufweite
#define DIR_DISTANCE 10000
#define DIR_SW 1
#define DIR_S 2
#define DIR_SE 3
#define DIR_W 4
#define DIR_STOP 5
#define DIR_E 6
#define DIR_NW 7
#define DIR_N 8
#define DIR_NE 9

int PrevDirection = DIR_STOP;

Die eigentliche Funktion zur Tastatursteuerung sieht folgendermaßen aus:

function Tastatursteuerung() {
// --- Tastatursteuerung ---
int Direction, dx, dy;

// Neue Richtung ermitteln
if ((IsKeyPressed (371) == 1) || (IsKeyPressed (55) == 1)) Direction = DIR_NW;
else if ((IsKeyPressed (372) == 1) || (IsKeyPressed (56) == 1)) Direction = DIR_N;
else if ((IsKeyPressed (373) == 1) || (IsKeyPressed (57) == 1)) Direction = DIR_NE;
else if ((IsKeyPressed (375) == 1) || (IsKeyPressed (52) == 1)) Direction = DIR_W;
else if ((IsKeyPressed (377) == 1) || (IsKeyPressed (54) == 1)) Direction = DIR_E;
else if ((IsKeyPressed (379) == 1) || (IsKeyPressed (49) == 1)) Direction = DIR_SW;
else if ((IsKeyPressed (380) == 1) || (IsKeyPressed (50) == 1)) Direction = DIR_S;
else if ((IsKeyPressed (381) == 1) || (IsKeyPressed (51) == 1)) Direction = DIR_SE;
else Direction = DIR_STOP;

// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
if (Direction == DIR_STOP)
{
StopMoving(EGO);
} // Stopp der Spielfigur
else
{
if (Direction == DIR_NW) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; }
else if (Direction == DIR_N) { dx = 0; dy = -DIR_DISTANCE; }
else if (Direction == DIR_NE) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; }
else if (Direction == DIR_W) { dx = -DIR_DISTANCE; dy = 0; }
else if (Direction == DIR_E) { dx = DIR_DISTANCE; dy = 0; }
else if (Direction == DIR_SW) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; }
else if (Direction == DIR_S) { dx = 0; dy = DIR_DISTANCE; }
else if (Direction == DIR_SE) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; }
MoveCharacterStraight (EGO, character[EGO].x + dx, character[EGO].y + dy);
}
}
}
und so gehts:

Source/Subs.php
function preparsecode(), ~ 357
direkt unter
global settings, $ext
//-- begin Code-Highlight-Hack by theDon
   $message = cpp_parse($message);
   $message = ags_parse($message);
   //-- end Code-Highlight-Hack by theDon
etwas darüber, außerhalb der function preparsecode()
Wer Ironie oder beleidigende Inhalte in diesem Beitrag findet, darf sie behalten.

http://www.thwboard.de | http://oph-qfb.sourceforge.net
theDon
Rätselmeister
Rätselmeister
Beiträge: 2219
Registriert: 11.06.2002, 18:22
Wohnort: gone (forever)
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von theDon »

Code: Alles auswählen

//-- begin Code-Highlight-Hack by theDon
/***
* cpp2thwb (c) 2001-2002, TheTinySteini and dp, apapted to YabbSE by theDon
* Highlights C++-Code with colors set at the beginning of the function.
* Uses many regular expressions that will eat up quite a bit of cpu time, so use with care.
* Far from being perfect, but you won't really notice =)
***/
function cpp2thwb( $string )
{
   // this is here for debugging so you can see how much time it takes on your server
   // to highlight the code
//    $starttime = microtime();
   
   // ADJUST THE COLORS HERE
   $color['comments'] = 'LimeGreen'; // /* */ or //
   $color['string'] = 'Purple';   // "strings"
   $color['numbers'] = 'Brown';  // 123456.789
   $color['types'] = 'Blue';    // int, void, true and related
   $color['keywords'] = 'Navy'; // for, if, return and related
   
   // Begin extracting =================
   // Comments and strings are extracted before parsing keywords and then
   // reinserted later. I really don't like this, but as it's pretty fast,
   // I left it untouched.
   // block comments
   if(preg_match_all("/\/\*(.*)\*\//Us", $string, $cmts))
   {
      $string = preg_replace("/\/\*(.*)\*\//Us","|%|C|%|",$string);
   }
   // line comments
   if(preg_match_all("/\/\/.*\n/U", $string, $lcmts))
   {
      $string = preg_replace("/\/\/(.*)\n/U", "|%|LC|%|", $string);
   }
   // strings
   if(preg_match_all("/\"(.*)\"/Us", $string, $strs))
   {
      $string = preg_replace("/\"(.*)\"/Us","|%|S|%|",$string);
   }
   // End extracting ===================
   
   // Now parse the rest ===============
   // this is the really slow part, might take some 1/10ths of a second
   // Numbers
   $string = preg_replace('/([^a-zA-Z0-9])([0-9.]{1,})/',"\\1[color=$color[numbers]]\\2[/color]",$string);
   // reserved words
   // these are blue
   $string = preg_replace("/(bool|char|wchar_t|class|const|double|enum|float|int|long|private:?|protected:?|public:?|short|signed|static|struct|template|typedef|unsigned|virtual|void|namespace|using|this|sizeof|true|false|#include|#define)([ \t\r\n;,*)])/","[color=$color[types]]\\1[/color]\\2",$string);
   // these are darkblue
   $string = preg_replace("/(auto|break|case|catch|continue|default|delete|do|else|extern|for|friend|goto|if|inline|mutable|new|operator|register|return|switch|throw|union|volatile|while|asm|cdecl|pascal|near|far)([( \t\r\n;])/","[color=$color[keywords]][b]\\1[/b][/color]\\2",$string);
   // End parsing ======================
   
   // Begin reinserting ================
   // Reinsert the comments and strings that were extracted earlier
   $i = 0;
   while(true)
   {
      $pos = strpos($string, "|%|LC|%|");
      if(!is_integer($pos)) break; // no match
      $string = substr($string, 0, $pos)."[color=$color[comments]]".$lcmts[0][$i]."[/color]".substr($string, $pos + 8);
      $i++;
   }
   
   $i = 0;
   while(true)
   {
      $pos = strpos($string, "|%|C|%|");
      if(!is_integer($pos)) break; // no match
      $string = substr($string, 0, $pos)."[color=$color[comments]]".$cmts[0][$i]."[/color]".substr($string, $pos + 7);
      $i++;
   }
   
   $i = 0;
   while(true)
   {
      $pos = strpos($string, "|%|S|%|");
      if(!is_integer($pos)) break; // no match
      $string = substr($string, 0, $pos)."[color=$color[string]]".$strs[0][$i]."[/color]".substr($string, $pos + 7);
      $i++;
   }
   
   // End reinserting ==================
   
   // again for debugging purposes - rip this out when satisfied =)
//    $endtime = microtime();
//    $end_t = explode( " ", $endtime );
//    $start_t = explode( " ", $starttime );
//    $total = ($end_t[1] - $start_t[1]) + $end_t[0] - $start_t[0];
   
   return $string; // ."\n[b]Code processed in $total seconds.[/b]";
}

function ags2thwb( $string )
{
   // this is here for debugging so you can see how much time it takes on your server
   // to highlight the code
//    $starttime = microtime();
   
   // ADJUST THE COLORS HERE
   $color['comments'] = 'LimeGreen'; // /* */ or //
   $color['string'] = 'Purple';   // "strings"
   $color['numbers'] = 'Brown';  // 123456.789
   $color['types'] = 'Blue';    // int, void, true and related
   $color['keywords'] = 'Navy'; // for, if, return and related
   
   // Begin extracting =================
   // Comments and strings are extracted before parsing keywords and then
   // reinserted later. I really don't like this, but as it's pretty fast,
   // I left it untouched.

   // line comments
   if(preg_match_all("/\/\/.*\n/U", $string, $lcmts))
   {
      $string = preg_replace("/\/\/(.*)\n/U", "|%|LC|%|", $string);
   }
   // strings
   if(preg_match_all("/\"(.*)\"/Us", $string, $strs))
   {
      $string = preg_replace("/\"(.*)\"/Us","|%|S|%|",$string);
   }
   // End extracting ===================
   
   // Now parse the rest ===============
   // this is the really slow part, might take some 1/10ths of a second
   // Numbers
   $string = preg_replace('/([^a-zA-Z0-9])([0-9.]{1,})/',"\\1[color=$color[numbers]]\\2[/color]",$string);
   // reserved words
   // these are blue
   $string = preg_replace("/(function|int|string|#define)([ \t\r\n;,*)])/","[color=$color[types]]\\1[/color]\\2",$string);
   // these are darkblue
   $string = preg_replace("/(break|case|continue|default|do|else|for|goto|if|return|switch|while)([( \t\r\n;])/","[color=$color[keywords]][b]\\1[/b][/color]\\2",$string);
   // End parsing ======================
   
   // Begin reinserting ================
   // Reinsert the comments and strings that were extracted earlier
   $i = 0;
   while(true)
   {
      $pos = strpos($string, "|%|LC|%|");
      if(!is_integer($pos)) break; // no match
      $string = substr($string, 0, $pos)."[color=$color[comments]]".$lcmts[0][$i]."[/color]".substr($string, $pos + 8);
      $i++;
   }
   
          $i = 0;
   while(true)
   {
      $pos = strpos($string, "|%|S|%|");
      if(!is_integer($pos)) break; // no match
      $string = substr($string, 0, $pos)."[color=$color[string]]".$strs[0][$i]."[/color]".substr($string, $pos + 7);
      $i++;
   }
   
   // End reinserting ==================
   
   // again for debugging purposes - rip this out when satisfied =)
//    $endtime = microtime();
//    $end_t = explode( " ", $endtime );
//    $start_t = explode( " ", $starttime );
//    $total = ($end_t[1] - $start_t[1]) + $end_t[0] - $start_t[0];
   
   return $string; // ."\n[b]Code processed in $total seconds.[/b]";
}

function cpp_parse( $string )
{
   // make sure we don't run this in PHP < 4.0
   if( floor(phpversion()) > 3 )
   { 
      // somewhat hacky code to extract the [cpp]-block, parse it and reinsert it.
      // might be better/faster to mess around with strpos and substr instead of
      // all this preg stuff
      if( preg_match_all( "/\[cpp\](.*)\[\/cpp\]/Us", $string, $cpp ) )
      {
         $string = preg_replace("/\[cpp\].*\[\/cpp\]/Us", "|%|CPP|%|", $string );
         for( $i = 0; $i < count($cpp[0]); $i++ )
         {
                   $cpp[0][$i] = str_replace('[cpp]', '[quote]', $cpp[0][$i]);
              $cpp[0][$i] = str_replace('[/cpp]', '[/quote]', $cpp[0][$i]);
            $pos = strpos($string, "|%|CPP|%|");
            $string = substr($string, 0, $pos). cpp2thwb($cpp[0][$i]) .substr($string, $pos + 9);
         }
      }
   }
   
   return $string;
}

function ags_parse( $string )
{
   // make sure we don't run this in PHP < 4.0
   if( floor(phpversion()) > 3 )
   { 
      // somewhat hacky code to extract the [ags]-block, parse it and reinsert it.
      // might be better/faster to mess around with strpos and substr instead of
      // all this preg stuff
      if( preg_match_all( "/\[ags\](.*)\[\/ags\]/Us", $string, $ags ) )
      {
         $string = preg_replace("/\[ags\].*\[\/ags\]/Us", "|%|AGS|%|", $string );
         for( $i = 0; $i < count($ags[0]); $i++ )
         {
                   $ags[0][$i] = str_replace('[ags]', '[quote]', $ags[0][$i]);
              $ags[0][$i] = str_replace('[/ags]', '[/quote]', $ags[0][$i]);
            $pos = strpos($string, "|%|AGS|%|");
            $string = substr($string, 0, $pos). ags2thwb($ags[0][$i]) .substr($string, $pos + 9);
         }
      }
   }
   
   return $string;
}
//-- end Code-Highlight-Hack by theDon
Wer Ironie oder beleidigende Inhalte in diesem Beitrag findet, darf sie behalten.

http://www.thwboard.de | http://oph-qfb.sourceforge.net
Benutzeravatar
DasJan
Adventure-Treff
Adventure-Treff
Beiträge: 14683
Registriert: 17.02.2002, 17:34
Wohnort: London
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von DasJan »

Hey, das sieht gut aus! Und all die feinen RegExps ;)

Ich kann mal bei Gelegenheit die ganzen Keywords von AGS da reinschreiben. Wie ist das: Highlighted der nur in quote-Bereichen? Danke jedenfalls für den Code...

Das Jan
"If you are the smartest person in the room, you are in the wrong room."
theDon
Rätselmeister
Rätselmeister
Beiträge: 2219
Registriert: 11.06.2002, 18:22
Wohnort: gone (forever)
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von theDon »

nö, hightlighted innerhalb von [ ags ] und [ /ags ] sowie [ cpp ] und [ /cpp ]
/edit
da steht zitat: weil es mir zu aufwendig war ein neues tag einzubauen (sch** yabbse code ist _unübersichtlich_, da arbeite ich lieber mit thwb ^^) und ich kein [ code ] benutzen kann weil innerhalb davon keine [ color ] geparsed werden.
edit/
Wer Ironie oder beleidigende Inhalte in diesem Beitrag findet, darf sie behalten.

http://www.thwboard.de | http://oph-qfb.sourceforge.net
Benutzeravatar
DasJan
Adventure-Treff
Adventure-Treff
Beiträge: 14683
Registriert: 17.02.2002, 17:34
Wohnort: London
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von DasJan »

Gut zu wissen, thx!

Das Jan
"If you are the smartest person in the room, you are in the wrong room."
Benutzeravatar
DasJan
Adventure-Treff
Adventure-Treff
Beiträge: 14683
Registriert: 17.02.2002, 17:34
Wohnort: London
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von DasJan »

Test Test Test...
// --- Konstanten für Richtungen und Laufweite
#define DIR_DISTANCE 10000
#define DIR_SW 1
#define DIR_S 2
#define DIR_SE 3
#define DIR_W 4
#define DIR_STOP 5
#define DIR_E 6
#define DIR_NW 7
#define DIR_N 8
#define DIR_NE 9

int PrevDirection = DIR_STOP;

Die eigentliche Funktion zur Tastatursteuerung sieht folgendermaßen aus:

function Tastatursteuerung() {
// --- Tastatursteuerung ---
int Direction, dx, dy;

// Neue Richtung ermitteln
if ((IsKeyPressed (371) == 1) || (IsKeyPressed (55) == 1)) Direction = DIR_NW;
else if ((IsKeyPressed (372) == 1) || (IsKeyPressed (56) == 1)) Direction = DIR_N;
else if ((IsKeyPressed (373) == 1) || (IsKeyPressed (57) == 1)) Direction = DIR_NE;
else if ((IsKeyPressed (375) == 1) || (IsKeyPressed (52) == 1)) Direction = DIR_W;
else if ((IsKeyPressed (377) == 1) || (IsKeyPressed (54) == 1)) Direction = DIR_E;
else if ((IsKeyPressed (379) == 1) || (IsKeyPressed (49) == 1)) Direction = DIR_SW;
else if ((IsKeyPressed (380) == 1) || (IsKeyPressed (50) == 1)) Direction = DIR_S;
else if ((IsKeyPressed (381) == 1) || (IsKeyPressed (51) == 1)) Direction = DIR_SE;
else Direction = DIR_STOP;

// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
if (Direction == DIR_STOP)
{
StopMoving(EGO);
} // Stopp der Spielfigur
else
{
if (Direction == DIR_NW) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; }
else if (Direction == DIR_N) { dx = 0; dy = -DIR_DISTANCE; }
else if (Direction == DIR_NE) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; }
else if (Direction == DIR_W) { dx = -DIR_DISTANCE; dy = 0; }
else if (Direction == DIR_E) { dx = DIR_DISTANCE; dy = 0; }
else if (Direction == DIR_SW) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; }
else if (Direction == DIR_S) { dx = 0; dy = DIR_DISTANCE; }
else if (Direction == DIR_SE) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; }
MoveCharacterStraight (EGO, character[EGO].x + dx, character[EGO].y + dy);
}
}
}
"If you are the smartest person in the room, you are in the wrong room."
Benutzeravatar
DasJan
Adventure-Treff
Adventure-Treff
Beiträge: 14683
Registriert: 17.02.2002, 17:34
Wohnort: London
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von DasJan »

Hey, das macht Spaß! :)
// called when a key is pressed. keycode holds the key's ASCII code
if (IsGamePaused() == 1) keycode=0; // game paused, so don't react to keypresses
if (keycode==17) QuitGame(1); // Ctrl-Q
if (keycode==363) SaveGameDialog(); // F5
if (keycode==365) RestoreGameDialog(); // F7
if (keycode==367) RestartGame(); // F9
if (keycode==434) SaveScreenShot("scrnshot.bmp"); // F12
if (keycode==9) InventoryScreen(); // Tab, show inventory
if (keycode==19) Debug(0,0); // Ctrl-S, give all inventory
if (keycode==22) Debug(1,0); // Ctrl-V, version
if (keycode==1) Debug(2,0); // Ctrl-A, show walkable areas
if (keycode==24) Debug(3,0); // Ctrl-X, teleport to room
Das Jan
"If you are the smartest person in the room, you are in the wrong room."
theDon
Rätselmeister
Rätselmeister
Beiträge: 2219
Registriert: 11.06.2002, 18:22
Wohnort: gone (forever)
Kontaktdaten:

Re:YabbSE-Hack: C/C++ & AGS-Syntax-Highlighting

Beitrag von theDon »

int main(int argc, char** argv, char** env)
{
/* Der Sinn des Lebens, des Universums und überhaupt */
return 42;
}
Wer Ironie oder beleidigende Inhalte in diesem Beitrag findet, darf sie behalten.

http://www.thwboard.de | http://oph-qfb.sourceforge.net
Antworten