Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUTORIAL] How to create a command - DarkPlaces engine C programming

#1
Hello, all! Smile
It's the first time I've touched engine code to test something simple.
IMPORTANT NOTE: only modifies engine part.

1. Open a file called console.c in xonotic/darkplaces folder

2. Write the following in the line 842 (NOTE: This line is specific, if you see something not empty, don't do. The code may change in some time due to the development, so don't write inside the functions, write it outside the functions)
Code:
//LegendGuard first time using engine code 27-03-2021
/*
================
Con_Hello_f
================
*/
static void Con_Hello_f (void)
{
   Con_Printf("HELLO XONOTIC! I'm LegendGuard, touching console code in the engine! :D\n");
   return;
}


3. Write the following line to call the command function in the line 917 more or less where says "// register our commands":
Code:
Cmd_AddCommand ("hello", Con_Hello_f, "output a hello message");


4. Save console.c file and go to console MSYS2 (as I use Windows)



5. In the console, while you're in xonotic/darkplaces directory, write and execute  make sdl-release 
(optionally, you can use  ./all compile -r   when you're in xonotic directory but IT'S NOT RECOMMENDED, since it compiles all contents and is slow to check the results of your code in the engine)

NOTE: about  make
sdl-release = xonotic-sdl
sv-release = xonotic-dedicated
You can compile graphical with glx or sdl interface or non graphical dedicated good for servers without a windowing system.

There are differences:
make sdl-release    uses sdl2-config (in my system is version 2.0.12)  (uses -ljpeg)   - USE THIS COMMAND IN xonotic/darkplaces DIRECTORY -
./all compile sdl   uses xonotic/misc/builddeps/win64/sdl/bin/sdl2-config (version 2.0.10)  (doesn't use -ljpeg)   - USE THIS COMMAND IN xonotic DIRECTORY -

You can see more info in the makefile

6. After compiled successfully, go to xonotic directory and run the game with ./all run


7. Press Shift + Esc, type  hello  and ENJOY!!!  Cool 

[Image: darkplacesconsole.jpg]
Reply

#2
In this section, it will show you how to develop a Basic Calculator command:

There are things to keep in mind: 
  • It's convenient to understand C programming problems. Functions like strcmp() has been used in this part.
  • Some operations won't have the same result like the other tutorial, the code isn't the same.
  • Takes effort improving the structure where we want to develop.
    With a little help, you can write those lines and look what happens to the parameters/variables:
    Code:
    Con_Printf("^1DEBUG: ^5Cmd_Argc() is: ^6%d\n", Cmd_Argc()); //number of parameters inserted, if there is no parameters, will be zero

    Con_Printf("^1DEBUG: ^5Cmd_Argv(0) is: ^6%s\n", Cmd_Argv(0)); //command where execute or main parameter

    Con_Printf("^1DEBUG: ^5Cmd_Argv(1) is: ^6%s\n", Cmd_Argv(1)); //first parameter

    Con_Printf("^1DEBUG: ^5Cmd_Argv(2) is: ^6%s\n", Cmd_Argv(2)); //second parameter

    Con_Printf("^1DEBUG: ^5Cmd_Argv(3) is: ^6%s\n", Cmd_Argv(3)); //third parameter

Requires having learned something from it, anyway, let's get started!


1. Open a file called console.c in xonotic/darkplaces folder

2. Write the following in the line 843 (NOTE: This line is specific, if you see something not empty, don't do. The code may change in some time due to the development, so don't write inside the functions, write it outside the functions)

Code:
/*
================
Con_Calc_f
================
*/
void InfoCalcParameters (void);
void InfoCalcParameters (void)
{
  char modulussign[] = "%";
  Con_Printf("Usage:^3 calc\n");
  Con_Printf("  Where '+', '-', '*', '/', '^' and '%s' are the operators to calculate.\n", modulussign);
  Con_Printf("  Examples:\n^3 calc 4 + 5\n^3 calc 3 ^ 2\n^3 calc 8 %s 2\n ^3calc pi * 0.5\n^3 calc 32.46 / π\n^3 calc τ * 0.112\n^3 calc 4.1 / tau\n", modulussign);
}

static void Con_Calc_f (void)
{
  int pipar1, pisignpar1, pipar3, pisignpar3, taupar1, tausignpar1, taupar3, tausignpar3,
  op_add, op_sub, op_mult, op_div, op_exp, op_mod;
  float result, numf1, numf2;

  numf1 = atof(Cmd_Argv(1));
  numf2 = atof(Cmd_Argv(3));

  pipar1 = strcmp(Cmd_Argv(1), "pi");
  pisignpar1 = strcmp(Cmd_Argv(1), "π");

  pipar3 = strcmp(Cmd_Argv(3), "pi");
  pisignpar3 = strcmp(Cmd_Argv(3), "π");

  taupar1 = strcmp(Cmd_Argv(1), "tau");
  tausignpar1 = strcmp(Cmd_Argv(1), "τ");

  taupar3 = strcmp(Cmd_Argv(3), "tau");
  tausignpar3 = strcmp(Cmd_Argv(3), "τ");

  op_add = strcmp(Cmd_Argv(2), "+");
  op_sub = strcmp(Cmd_Argv(2), "-");
  op_mult = strcmp(Cmd_Argv(2), "*");
  op_div = strcmp(Cmd_Argv(2), "/");
  op_exp = strcmp(Cmd_Argv(2), "^");
  op_mod = strcmp(Cmd_Argv(2), "%");

  if ((Cmd_Argv(1) != NULL && Cmd_Argv(2) != NULL && Cmd_Argv(3) != NULL) ||
  numf1 != atof("") || numf2 != atof(""))// || numf1 == atof(Cmd_Argv(1) || numf2 == atof(Cmd_Argv(3))
  {
     //if the parameter is pi number
     if (pipar1 == 0 || pisignpar1 == 0) //if user writes pi in the first parameter to calculate using pi number
         numf1 = 3.14159265358979323;
     if (pipar3 == 0 || pisignpar3 == 0) //if user writes pi in the third parameter to calculate using pi number
         numf2 = 3.14159265358979323;

     //if the parameter is tau number
     if (taupar1 == 0 || tausignpar1 == 0) //if user writes tau in the first parameter to calculate using tau number
         numf1 = 2 * 3.14159265358979323;
     if (taupar3 == 0 || tausignpar3 == 0) //if user writes tau in the third parameter to calculate using tau number
         numf2 = 2 * 3.14159265358979323;

     if (op_add == 0)
     {
         result = numf1 + numf2;
         Con_Printf("^5The sum result is: ^6%f\n", result);
         return;
     }
     else if (op_sub == 0)
     {
         result = numf1 - numf2;
         Con_Printf("^5The substraction result is: ^6%f\n", result);
         return;
     }
     else if (op_mult == 0)
     {
         result = numf1 * numf2;
         Con_Printf("^5The multiplication result is: ^6%f\n", result);
         return;
     }
     else if (op_div == 0)
     {
         result = numf1 / numf2;
         Con_Printf("^5The division result is: ^6%f\n", result);
         return;
     }
     else if (op_exp == 0)
     {
         result = pow(numf1, numf2);
         Con_Printf("^5The exponent result is: ^6%f\n", result);
         return;
     }
     else if (op_mod == 0)
     {
         result = fmod(numf1, numf2);
         Con_Printf("^5The modulus result is: ^6%f\n", result);
         return;
     }
     else
     {
         InfoCalcParameters();
         return;
     }
  }
  else
  {
     InfoCalcParameters();
     return;
  }
}

3. Write the following line to call the command function in the line 1019 more or less where says "// register our commands" inside "Con_Init" function:
Code:
Cmd_AddCommand ("calc", Con_Calc_f, "basic calculator, input parameters to get a result: [num1] [operator] [num2]");

4. Save console.c file and go to console MSYS2 (as I use Windows)


5. In the console, while you're in xonotic/darkplaces directory, write and execute  make sdl-release 
(optionally, you can use  ./all compile -r   when you're in xonotic directory but IT'S NOT RECOMMENDED, since it compiles all contents and is slow to check the results of your code in the engine)

NOTE: about  make
sdl-release = xonotic-sdl
sv-release = xonotic-dedicated
You can compile graphical with glx or sdl interface or non graphical dedicated good for servers without a windowing system.

There are differences:
make sdl-release    uses sdl2-config (in my system is version 2.0.12)  (uses -ljpeg)   - USE THIS COMMAND IN xonotic/darkplaces DIRECTORY -
./all compile sdl   uses xonotic/misc/builddeps/win64/sdl/bin/sdl2-config (version 2.0.10)  (doesn't use -ljpeg)   - USE THIS COMMAND IN xonotic DIRECTORY -

You can see more info in the makefile

6. After compiled successfully, go to xonotic directory and run the game with ./all run

7. Press Shift + Esc, use calc  command and ENJOY!!  [Image: cool.png] 

[Image: dpcalc.jpg]
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand darkplaces source code wiefie 23 17,619 06-08-2024, 11:40 AM
Last Post: dagelf
  Xonotic 0.8.5/DarkPlaces "Issues" Baker 2 1,313 02-24-2023, 03:01 PM
Last Post: Baker
  [META] DarkPlaces fork Lyberta 24 17,119 02-21-2023, 07:12 PM
Last Post: ballerburg9005
  [META] Xonotic reboot/port in modern engine ballerburg9005 32 14,072 02-22-2022, 05:11 PM
Last Post: ballerburg9005
  What was easy for you in development? (Darkplaces and QuakeC programming) LegendGuard 2 3,059 08-08-2020, 05:25 PM
Last Post: LegendGuard
  [TUTORIAL] How to create a command - Xonotic QuakeC programming LegendGuard 3 3,702 07-25-2020, 06:24 PM
Last Post: LegendGuard
  Engine: thread handling kingtiger01 0 3,412 11-06-2015, 12:57 AM
Last Post: kingtiger01
  Engine: cpu extensions kingtiger01 0 3,010 11-06-2015, 12:39 AM
Last Post: kingtiger01
  Ongoing port to the Unvanquished engine? poVoq 9 14,505 11-05-2015, 11:09 PM
Last Post: Danfun64
  Module (music) support for Darkplaces (again) [test it] nilyt 8 9,091 04-21-2015, 08:24 PM
Last Post: BuddyFriendGuy

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB original theme © iAndrew 2016, remixed by -z-