Wednesday, April 27, 2011

C++ Tutorial: Create a DLL in VC++ 2010/2008

If the tutorial is too fast for you then read the following steps:

1. Start Visual Studio (or C++ Express Edition) -> Create a new Win32 Console Application project -> Application Settings -> Check DLL and Empy Project -> Hit Finish

2. Go to Solution Explorer -> Right Click on Header Files -> Add a new header file -> Declare your stuff (for example: a new class)

// SimpleH.h
namespace nmspace
{
 class myclass
 {
 public:
  static __declspec(dllexport) void Crap();
 };
}

NOTE: The dllexport and dllimport storage-class attributes are Microsoft-specific extensions to the C and C++ languages. They enable you to export and import functions, data, and objects to and from a DLL.

3. Go to Solution Explorer -> Right Click on Source Files -> Add a new cpp file -> Include your header here #include "YourHeader.h" and define your stuff

// SimpleCPP.cpp
#include <iostream>
using namespace std;
#include "SimpleH.h"

namespace nmspace
{
 void myclass::Crap()
 {
  cout << "I'm called within a crappy DLL!";
 }
}

4. Create a new Win32 Console Application project -> Application Settings -> Leave Console application checked and check Empty Project -> Add a new CPP file (for example: main.cpp)

5. Go to your DLL Project folder and copy the Header file (SimpleH.h), the Object Library File (SimpleDLL.lib) and the DLL itself (SimpleDLL.dll)! Paste these files into your new project folder (where main.cpp is located)

6. Include the Header file (SimpleH.h) to your new project -> Go to Solution Explorer -> Right Click on the Project name (NOT THE SOLUTION) -> References

7. Configuration Properties -> Linker -> Input -> Additional Dependencies -> Add the .lib file (SimpleDLL.lib) -> Hit OK -> HIT APPLY and then OK!

8. In main.cpp call the function from the DLL created -> Start the application

// main.cpp
#include "SimpleH.h"
#include <iostream>
using namespace std;
using namespace nmspace;

int main()
{
 myclass::Crap();
 system("PAUSE");
}

9. Go to you project folder -> Debug folder (The first Debug folder) -> Copy the DLL there -> Start the application -> And you're done!

10. If you found this confusing then watch the video tutorial!

Saturday, April 23, 2011

SAMP Scripting Tutorial: Slap N' Slay

Here is the code

#include <a_samp>

#define RED 0xD20000FF
#define WHITE 0xFFFFFFFF

public OnFilterScriptInit()
{
 print("\n--------------------------------------");
 print(" Slap N' Slay by TheDarkJoker94");
 print("--------------------------------------\n");
 return 1;
}

// The strtok function
strtok(const string[], &index)
{
 new length = strlen(string);
 while ((index < length) && (string[index] <= ' '))
 {
  index++;
 }

 new offset = index;
 new result[20];
 while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
 {
  result[index - offset] = string[index];
  index++;
 }
 result[index - offset] = EOS;
 return result;
}

// The Slap function
Slap(victimid, victimhp, adminid)
{
 new Float: health;
 GetPlayerHealth(victimid, health); // Get victim's health
 health = health - victimhp; // Calculate new health (victimhp is the ammount of HP you want to take from the victim)
 SetPlayerHealth(victimid, health);
 new name[64], output[128];
 GetPlayerName(adminid, name, 64); // Get slapper's name
 format(output, 128, "You have been slapped by %s! [-%d]", name, victimhp);
 SendClientMessage(victimid, RED, output); // Send the message to the victim
 new Float:x,Float:y,Float:z; 
 GetPlayerPos(victimid, x,y,z); // Get victim's position
 PlayerPlaySound(victimid, 1130, x, y, z); // Play the sound at that position
 SetPlayerVelocity(victimid, 0.0, 0.0, 0.2); // A 0.2 velocity in the Z direction will make the player to jump
}

public OnPlayerCommandText(playerid, cmdtext[])
{
 new cmd[128], tmp[28], vicid, vichp, idx;
 cmd = strtok(cmdtext, idx);
 if (strcmp("/slap", cmd, true) == 0)
 {
  tmp = strtok(cmdtext, idx);
  if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slap [VictimID] [AmmountHP]");
  vicid = strval(tmp);
        tmp = strtok(cmdtext, idx);
        if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slap [VictimID] [AmmountHP]");
        vichp = strval(tmp);
        Slap(vicid, vichp, playerid);
  return 1;
 }
 else if (strcmp("/slay", cmd, true) == 0)
 {
     tmp = strtok(cmdtext, idx);
  if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slay [VictimID]");
  vicid = strval(tmp);
  SetPlayerHealth(vicid, 0.0);
  new name[64], output[128];
  GetPlayerName(playerid, name, 64);
  format(output, 128, "You have been slayed by %s!", name);
  SendClientMessage(vicid, RED, output);
  return 1;
 }
 return 0;
}