Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
DLL Projects:
Browse files Browse the repository at this point in the history
- TIMER.DLL: Created advanced timers DLL:
    - Allow up to 64 timers.
    - Allow to pause timers.
    - Allow to ease reset timers.
    - Timers are independent from DIV timers.
- TEST.DLL: Updated test DLL.
  • Loading branch information
José Miguel Sánchez Fernández committed May 30, 2020
1 parent 03813d4 commit f6d9cec
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 14 deletions.
Binary file modified DLL/TEST.DLL
Binary file not shown.
42 changes: 28 additions & 14 deletions DLL/TEST/TEST.CPP
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define GLOBALS
#include "..\div.h"

char *upr(const char *input);
int startTime;

void getText()
{
//char *text = "This is a test!";
char *text = (char *)malloc(sizeof(char) * 15);
text = "This is a test!";

char *test = upr(text);

char *dest = (char *)&mem[text_offset + getparm()];

//strcpy(dest, test);
memcpy(dest, test, strlen(test) - 1);

void initTimer()
{
startTime = clock();
retval(0);
}

void getTime()
{
retval(clock() - startTime);
}

char *upr(const char *input)
{
int len = strlen(input);
Expand All @@ -37,8 +33,26 @@ char *upr(const char *input)
return output;
}

void getText()
{
//char *text = "This is a test!";
char *text = (char *)malloc(sizeof(char) * 15);
text = "This is a test!";

char *test = upr(text);

char *dest = (char *)&mem[text_offset + getparm()];

//strcpy(dest, test);
memcpy(dest, test, strlen(test) - 1);

retval(0);
}

void __export divlibrary(LIBRARY_PARAMS)
{
COM_export("initTimer", initTimer, 0);
COM_export("testTime", getTime, 0);
COM_export("getText", getText, 1);
}

Expand Down
Binary file added DLL/TIMER.DLL
Binary file not shown.
1 change: 1 addition & 0 deletions DLL/TIMER/MAKE.BAT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wcl386 TIMER.CPP /l=div_dll -s
217 changes: 217 additions & 0 deletions DLL/TIMER/TIMER.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/* ----------------------------------------------------------------------------
* TIMER.DLL - Advanced timers for DIV Games Studio.
* (C) VisualStudioEX3, José Miguel Sánchez Fernández - 2020
* DIV Games Studio 2 (C) Hammer Technologies - 1998, 1999
* ---------------------------------------------------------------------------- */

#include "timer.h"

int isIndexValid(int index)
{
if (_isClamped(index, 0, MAX_CAPACITY))
{
return timers[index].active == 1;
}
return(FALSE);
}

/** Get max number of available timers.
*
* @return {int} - Returns the number of available timers.
*/
void getCapacity()
{
retval(MAX_CAPACITY);
}

/** Get current number of active timers.
*
* @return {int} - Returns the current number of active timers.
*/
void getCount()
{
retval(count);
}

/** Create new timer.
*
* @return {int} - Returns the index of the new timer or RESULT_ERROR if not have available timers.
*/
void createTimer()
{
if (count < MAX_CAPACITY)
{
for (int i = 0; i < MAX_CAPACITY; i++)
{
struct TimerData* t = &timers[i];
if (!t->active)
{
t->active = 1;
t->startTime = clock();
count++;
retval(i);
return;
}
}
}

retval(RESULT_ERROR);
}

/** Free active timer.
*
* @param {int} index - Timer index.
*/
void freeTimer()
{
int index = getparm();

if (isIndexValid(index))
{
timers[index].active = 0;
count--;
retval(RESULT_OK);
return;
}

retval(RESULT_ERROR);
}

/** Free all timers. */
void freeAll()
{
for (int i = 0; i < MAX_CAPACITY; i++)
{
timers[i].active = 0;
count = 0;
}

retval(RESULT_OK);
}

/** Get current timer value.
*
* @param {int} index - Timer index.
*
* @return {int} - Returns the current timer value or RESULT_ERROR if the timer index is not valid or timer is not active.
*/
void getTime()
{
int index = getparm();

if (isIndexValid(index))
{
retval(timers[index].time);
return;
}

retval(RESULT_ERROR);
}

/** Update all active timers. */
void frameTimers()
{
struct TimerData* t;
for (int i = 0; i < MAX_CAPACITY; i++)
{
t = &timers[i];
t->time = t->paused ?
t->time = t->pauseDelta - t->startTime :
t->time = clock() - t->startTime;
}

retval(RESULT_OK);
}

/** Pause or resume active timer.
*
* @param {int} index - Timer index.
*
* @return {int} - Returns RESULT_ERROR if the timer index is not valid or timer is not active.
*/
void pause()
{
int index = getparm();

if (isIndexValid(index))
{
struct TimerData* t = &timers[index];
if (t->paused)
{
t->pauseDelta = clock();
t->paused = 1;
}
else
{
t->startTime = clock() - t->pauseDelta;
t->paused = 0;
}

retval(RESULT_OK);
return;
}

retval(RESULT_ERROR);
}

/** Is timer paused?
*
* @param {int} index - Timer index.
*
* @return {int} - Returns true if timer is paused or false if the timer is not paused or timer is not active.
*/
void isPaused()
{
int index = getparm();

if (isIndexValid(index))
{
retval(timers[index].paused);
return;
}

retval(FALSE); // Return false in case of error instead of RESULT_ERROR.
}

/** Reset active timer.
*
* @param {int} index - Timer index.
*
* @return {int} - Returns RESULT_ERROR if the timer index is not valid or timer is not active.
*/
void reset()
{
int paused = getparm();
int index = getparm();

if (isIndexValid(index))
{
struct TimerData* t = &timers[index];
t->startTime = clock();
t->pauseDelta = 0;

retval(RESULT_OK);
return;
}

retval(RESULT_ERROR);
}

void __export divlibrary(LIBRARY_PARAMS)
{
COM_export("get_timer_capacity", getCapacity, 0);
COM_export("get_timer_count", getCount, 0);
COM_export("create_timer", createTimer, 0);
COM_export("free_timer", freeTimer, 1);
COM_export("free_all_timers", freeAll, 0);
COM_export("get_time", getTime, 1);
COM_export("frame_timers", frameTimers, 0);
COM_export("pause_timer", pause, 1);
COM_export("is_timer_paused", isPaused, 1);
COM_export("reset_timer", reset, 1);
}

void __export divmain(COMMON_PARAMS)
{
GLOBAL_IMPORT();
}
33 changes: 33 additions & 0 deletions DLL/TIMER/TIMER.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ----------------------------------------------------------------------------
* TIMER.DLL - Advanced timers for DIV Games Studio.
* (C) VisualStudioEX3, José Miguel Sánchez Fernández - 2020
* DIV Games Studio 2 (C) Hammer Technologies - 1998, 1999
* ---------------------------------------------------------------------------- */

#include <time.h>
#include "..\common.h"

#define MAX_CAPACITY 64

struct TimerData
{
int active;
int startTime;
int pauseDelta;
int time;
int paused;
};

int count = 0;
struct TimerData timers[MAX_CAPACITY];

void getCapacity();
void getCount();
void createTimer();
void freeTimer();
void freeAll();
void getTime();
void frameTimers();
void pause();
void isPaused();
void reset();

0 comments on commit f6d9cec

Please sign in to comment.