Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds SerialHelper and AgsImVec2 objects. Long work to support styles.
- Loading branch information
1 parent
b2cc6ed
commit a7016c5
Showing
6 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2020 Érico Vieira Porto | ||
* | ||
* This program is free software. You can use and redistribute it | ||
* under the terms and conditions of the MIT License (see LICENCE). | ||
*/ | ||
|
||
#include <cmath> | ||
#include "AgsImVec2.h" | ||
|
||
AgsImVec2::AgsImVec2(float x, float y) { | ||
_x = x; | ||
_y = y; | ||
} | ||
|
||
AgsImVec2::~AgsImVec2(void) | ||
{ | ||
} | ||
|
||
void AgsImVec2::SetX(float x){ | ||
_x = x; | ||
} | ||
|
||
float AgsImVec2::GetX(){ | ||
return _x; | ||
} | ||
|
||
void AgsImVec2::SetY(float y){ | ||
_y = y; | ||
} | ||
|
||
float AgsImVec2::GetY(){ | ||
return _y; | ||
} | ||
|
||
AgsImVec2* AgsImVec2::Scale(float scale){ | ||
return new AgsImVec2(_x * scale, _y * scale); | ||
} | ||
|
||
float AgsImVec2::Length(){ | ||
return sqrtf(_x * _x + _y * _y); | ||
} | ||
|
||
float AgsImVec2::SquaredLength(){ | ||
return (_x * _x + _y * _y); | ||
} | ||
|
||
AgsImVec2* AgsImVec2::Add(AgsImVec2* agsImVec2) { | ||
if(agsImVec2 == nullptr) return new AgsImVec2(_x , _y ); | ||
|
||
return new AgsImVec2(_x + agsImVec2->GetX(), _y + agsImVec2->GetY()); | ||
} | ||
|
||
AgsImVec2* AgsImVec2::Sub(AgsImVec2* agsImVec2) { | ||
if(agsImVec2 == nullptr) return new AgsImVec2(_x , _y ); | ||
|
||
return new AgsImVec2(_x - agsImVec2->GetX(), _y - agsImVec2->GetY()); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
|
||
extern IAGSEngine* engine; | ||
|
||
AgsImVec2Interface AgsImVec2_Interface; | ||
AgsImVec2Reader AgsImVec2_Reader; | ||
|
||
const char* AgsImVec2Interface::name = "AgsImVec2"; | ||
|
||
//------------------------------------------------------------------------------ | ||
#include "SerialHelper.h" | ||
using namespace SerialHelper; | ||
|
||
int AgsImVec2Interface::Dispose(const char* address, bool force) | ||
{ | ||
delete ((AgsImVec2*)address); | ||
return (1); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
int AgsImVec2Interface::Serialize(const char* address, char* buffer, int bufsize) | ||
{ | ||
AgsImVec2* agsimgui_imvec2 = (AgsImVec2*)address; | ||
char* ptr = buffer; | ||
char* end = buffer + bufsize; | ||
|
||
ptr = FloatToChar(agsimgui_imvec2->GetX(), ptr, end); | ||
ptr = FloatToChar(agsimgui_imvec2->GetY(), ptr, end); | ||
|
||
return (ptr - buffer); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
void AgsImVec2Reader::Unserialize(int key, const char* serializedData, int dataSize) | ||
{ | ||
char* ptr = (char*) serializedData; | ||
int agsimgui_imvec2_id = key; | ||
|
||
|
||
float val_x; | ||
float val_y; | ||
|
||
ptr = CharToFloat( val_x, ptr); | ||
ptr = CharToFloat( val_y, ptr); | ||
|
||
AgsImVec2* agsimgui_imvec2 = new AgsImVec2(val_x, val_y); | ||
|
||
engine->RegisterUnserializedObject(key, agsimgui_imvec2, &AgsImVec2_Interface); | ||
} | ||
|
||
//.............................................................................. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2020 Érico Vieira Porto | ||
* | ||
* This program is free software. You can use and redistribute it | ||
* under the terms and conditions of the MIT License (see LICENCE). | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef _AGSIMVEC2_H | ||
#define _AGSIMVEC2_H | ||
|
||
#include "plugin/agsplugin.h" | ||
|
||
class AgsImVec2 | ||
{ | ||
float _x = 0.0; | ||
float _y = 0.0; | ||
public: | ||
AgsImVec2(float x, float y); | ||
~AgsImVec2(void); | ||
void SetX(float x); | ||
float GetX(); | ||
void SetY(float y); | ||
float GetY(); | ||
|
||
AgsImVec2* Scale(float scale); | ||
float Length(); | ||
float SquaredLength(); | ||
AgsImVec2* Add(AgsImVec2* AgsImVec2); | ||
AgsImVec2* Sub(AgsImVec2* AgsImVec2); | ||
}; | ||
|
||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// AGS interface instances | ||
|
||
class AgsImVec2Interface : public IAGSScriptManagedObject | ||
{ | ||
public: | ||
static const char* name; | ||
|
||
AgsImVec2Interface() {}; | ||
|
||
virtual int Dispose(const char* address, bool force); | ||
virtual const char* GetType() { return (name); } | ||
virtual int Serialize(const char* address, char* buffer, int bufsize); | ||
|
||
}; | ||
|
||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|
||
class AgsImVec2Reader : public IAGSManagedObjectReader | ||
{ | ||
public: | ||
|
||
AgsImVec2Reader() {} | ||
|
||
virtual void Unserialize(int key, const char* serializedData, int dataSize); | ||
|
||
}; | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
extern AgsImVec2Interface AgsImVec2_Interface; | ||
extern AgsImVec2Reader AgsImVec2_Reader; | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
#endif /* _AGSIMVEC2_H */ | ||
|
||
//............ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2020 Érico Vieira Porto | ||
* | ||
* This program is free software. You can use and redistribute it | ||
* under the terms and conditions of the MIT License (see LICENCE). | ||
*/ | ||
|
||
#include "SerialHelper.h" | ||
#include <cassert> | ||
|
||
namespace SerialHelper { | ||
|
||
char* IntToChar(int i, char * buf, char* end) { | ||
assert(buf + sizeof(int) < end); | ||
|
||
*((int*)buf) = i; | ||
|
||
return buf + sizeof(int); | ||
} | ||
|
||
char* CharToInt(int &i, char * buf) { | ||
i = *((int*)buf); | ||
return buf + sizeof(int); | ||
} | ||
|
||
char* FloatToChar(float f, char * buf, char* end) { | ||
assert(buf + sizeof(float) < end); | ||
|
||
*((float*)buf) = f; | ||
|
||
return buf + sizeof(float); | ||
} | ||
|
||
char* CharToFloat(float &f, char * buf) { | ||
f = *((float*)buf); | ||
return buf + sizeof(float); | ||
} | ||
|
||
char* BoolToChar(bool b, char* buf, char* end) { | ||
assert(buf + sizeof(char) < end); | ||
|
||
if (b) { | ||
*buf = 1; | ||
} | ||
else { | ||
*buf = 0; | ||
} | ||
return buf + sizeof(char); | ||
} | ||
|
||
char* CharToBool(bool &b, char* buf) { | ||
if (*buf == 0) { | ||
b = false; | ||
} | ||
else { | ||
b = true; | ||
} | ||
return buf + sizeof(char); | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2020 Érico Vieira Porto | ||
* | ||
* This program is free software. You can use and redistribute it | ||
* under the terms and conditions of the MIT License (see LICENCE). | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef AGSBOX2D_SERIALHELPER_H | ||
#define AGSBOX2D_SERIALHELPER_H | ||
|
||
namespace SerialHelper { | ||
|
||
union u_cf | ||
{ | ||
float f; | ||
char s[sizeof(float)]; | ||
}; | ||
|
||
union u_ci | ||
{ | ||
int i; | ||
char s[sizeof(int)]; | ||
}; | ||
|
||
char* IntToChar(int i, char * buf, char* end); | ||
char* FloatToChar(float f, char * buf, char* end); | ||
char* BoolToChar(bool b, char* buf, char* end); | ||
|
||
char* CharToInt(int &i, char * buf); | ||
char* CharToFloat(float &f, char * buf); | ||
char* CharToBool(bool &b, char* buf); | ||
} | ||
|
||
#endif /* AGSBOX2D_SERIALHELPER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters