Skip to content

Commit

Permalink
adds SerialHelper and AgsImVec2 objects. Long work to support styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Jun 1, 2020
1 parent b2cc6ed commit a7016c5
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -48,6 +48,10 @@ add_library(agsimgui SHARED
agsimgui/imgui/examples/imgui_impl_dx9.cpp
agsimgui/imgui/examples/imgui_impl_dx9.h
agsimgui/agsimgui.h
agsimgui/AgsImVec2.cpp
agsimgui/AgsImVec2.h
agsimgui/SerialHelper.cpp
agsimgui/SerialHelper.h
agsimgui/agsimgui.cpp
agsimgui/Screen.cpp
agsimgui/Screen.h)
Expand Down
113 changes: 113 additions & 0 deletions agsimgui/AgsImVec2.cpp
@@ -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);
}

//..............................................................................
73 changes: 73 additions & 0 deletions agsimgui/AgsImVec2.h
@@ -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 */

//............
64 changes: 64 additions & 0 deletions agsimgui/SerialHelper.cpp
@@ -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);
}




}
36 changes: 36 additions & 0 deletions agsimgui/SerialHelper.h
@@ -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 */
4 changes: 4 additions & 0 deletions agsimgui/agsimgui.cpp
Expand Up @@ -39,6 +39,8 @@ struct IUnknown; // Workaround for "combaseapi.h(229): error C2187: syntax error
#include "Screen.h"
#include "libs/clip/clip.h"

#include "AgsImVec2.h"

#include <cstring>

#if defined(BUILTIN_PLUGINS)
Expand Down Expand Up @@ -1578,6 +1580,8 @@ int AgsImGuiHelper_GetClipboarImage() {
if (engine->version < MIN_ENGINE_VERSION)
engine->AbortGame("Plugin needs engine version " STRINGIFY(MIN_ENGINE_VERSION) " or newer.");

engine->AddManagedObjectReader(AgsImVec2Interface::name, &AgsImVec2_Reader);

//register functions
if(screen.driver == Screen::Driver::eOpenGL) {

Expand Down

0 comments on commit a7016c5

Please sign in to comment.