Initial commit
This commit is contained in:
845
application/source/application.c
Executable file
845
application/source/application.c
Executable file
@@ -0,0 +1,845 @@
|
||||
|
||||
|
||||
|
||||
// Classes
|
||||
|
||||
// #include <classConfiguration.h>
|
||||
|
||||
#define LINUX
|
||||
|
||||
#include <user.h>
|
||||
|
||||
#include "text.h"
|
||||
|
||||
#include "vector2.h"
|
||||
|
||||
#include "vector3.h"
|
||||
|
||||
#include "triangle.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "fileSystem.h"
|
||||
|
||||
#include "sqlite.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
#include "int.h"
|
||||
|
||||
#include "extends.h"
|
||||
|
||||
#include "toolset.h"
|
||||
|
||||
#include "console.h"
|
||||
|
||||
#include "street.h"
|
||||
|
||||
#include "opengl.h"
|
||||
|
||||
|
||||
// C includes
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
#include <sched.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// globals
|
||||
|
||||
char * someArray[10] = {"a", "b", "c"};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void startOpenGL() {
|
||||
|
||||
opengl * instance = new opengl();
|
||||
|
||||
instance->initialize();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
class thread{
|
||||
|
||||
int index;
|
||||
|
||||
int vectorCount;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void multiThread() {
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
printf("This system has %d threads configured and %d threads available.\n", get_nprocs_conf(), get_nprocs());
|
||||
|
||||
|
||||
int vectorCount = 10000;
|
||||
|
||||
int numberOfThreads = get_nprocs();
|
||||
|
||||
pthread_t thread[numberOfThreads];
|
||||
|
||||
for (int i = 0; i < numberOfThreads; ++i)
|
||||
{
|
||||
|
||||
struct thread data = new thread();
|
||||
|
||||
data.index = i;
|
||||
|
||||
data.vectorCount = vectorCount;
|
||||
|
||||
|
||||
pthread_create( &thread[i], NULL, mythread2, &data );
|
||||
|
||||
}
|
||||
|
||||
|
||||
//waiting for completion
|
||||
|
||||
for (int i = 0; i < numberOfThreads; ++i)
|
||||
{
|
||||
|
||||
pthread_join( thread[i], NULL );
|
||||
|
||||
printf("Thread %i has finished\n\n", i);
|
||||
|
||||
}
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("\n\nAll threads have finished\n\n");
|
||||
|
||||
|
||||
printf("Multiply: %f seconds, Threads: %i Vectors: %i\n\n\n", (double)( toc - tic ) / CLOCKS_PER_SEC, numberOfThreads, numberOfThreads * vectorCount );
|
||||
|
||||
|
||||
|
||||
printf("Now doing the same total number of vector additions on only the main thread.\n\n");
|
||||
|
||||
tic = clock();
|
||||
|
||||
|
||||
struct thread data = new thread();
|
||||
|
||||
data.index = 0;
|
||||
|
||||
data.vectorCount = numberOfThreads * vectorCount;
|
||||
|
||||
mythread2( &data );
|
||||
|
||||
|
||||
toc = clock();
|
||||
|
||||
printf("Multiply: %f seconds, Threads: 1 Vectors: %i\n", (double)( toc - tic ) / CLOCKS_PER_SEC, numberOfThreads * vectorCount );
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void * mythread2( void * args) {
|
||||
|
||||
struct thread * currentThread = ( struct thread * ) args;
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
int threadIndex = currentThread->index;
|
||||
|
||||
int vectorCount = currentThread->vectorCount;
|
||||
|
||||
|
||||
vector3 * newVector3 = new vector3();
|
||||
|
||||
newVector3->x = 0;
|
||||
|
||||
newVector3->y = 2;
|
||||
|
||||
newVector3->z = 3;
|
||||
|
||||
|
||||
for (int i = 0; i < vectorCount; ++i)
|
||||
{
|
||||
|
||||
vector3 vector3B = new vector3();
|
||||
|
||||
vector3B.x = 1;
|
||||
|
||||
vector3B.y = 3;
|
||||
|
||||
vector3B.z = 4;
|
||||
|
||||
|
||||
|
||||
vector3 vector3C = new vector3();
|
||||
|
||||
vector3C.x = 0;
|
||||
|
||||
vector3C.y = 3;
|
||||
|
||||
vector3C.z = 4;
|
||||
|
||||
//newVector3 = vector3C + ( vector3C + newVector3 + ( ( ( ( vector3B + vector3C ) ) + vector3C ) + vector3B ) );
|
||||
|
||||
newVector3 = newVector3 + ( ( vector3B + vector3C ) + vector3C );
|
||||
|
||||
//printf("thread %i: x: %i y: %i z: %i \n", threadIndex, newVector3->x, newVector3->y, newVector3->z);
|
||||
|
||||
}
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("\nThread .., threadIndex: %i finished, time: %f Computing %i * 2 vectors.\n\n\n", threadIndex, (double)( toc - tic ) / CLOCKS_PER_SEC, vectorCount);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void textAndArraysExamples() {
|
||||
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
text * sometext = new text(" my first text");
|
||||
|
||||
struct text * a = new text(" Mooi ");
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
|
||||
|
||||
sometext->appendObject( a )->append("something");
|
||||
|
||||
}
|
||||
|
||||
//printf("append: %s \n\n", sometext->buffer);
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("Multiply: %f seconds\n", (double)( toc - tic ) / CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
char * testText = "a,";
|
||||
|
||||
array * splitParts = testText->concatenate( "b, " )->concatenate("c,d,e")->concatenate(",f ")->split(",g");
|
||||
|
||||
char * compareString = "something";
|
||||
|
||||
splitParts.unshift((char *) "first");
|
||||
|
||||
splitParts.unshift((char *) "this first");
|
||||
|
||||
|
||||
printf( "joined: %s\n\n", splitParts.join(", ") );
|
||||
|
||||
char * lastElement = splitParts->pop();
|
||||
|
||||
printf( "pop joined: %s\n\n", splitParts.join(", ") );
|
||||
|
||||
printf( "pop removed element: %s\n\n", lastElement );
|
||||
|
||||
|
||||
if( compareString.compare("something") ) {
|
||||
|
||||
printf("this is true\n");
|
||||
|
||||
} else {
|
||||
|
||||
printf("this is not true\n");
|
||||
|
||||
}
|
||||
|
||||
printf("first: %s \n\n", splitParts.get( 0 ) );
|
||||
|
||||
int splitCount = splitParts->length();
|
||||
|
||||
printf("splitCount: %i\n", splitCount);
|
||||
|
||||
for (int i = 0; i < splitCount; ++i)
|
||||
{
|
||||
|
||||
char * splitPart = splitParts.get( i );
|
||||
|
||||
//printf("splitPart: %s \n\n", splitPart->trim() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void sqlite3Example() {
|
||||
|
||||
// sqlite * sql = new sqlite("./database/users.sqlite");
|
||||
|
||||
sqlite * sql = new sqlite(":memory:");
|
||||
|
||||
sql->selectModel( "user" );
|
||||
|
||||
|
||||
sql->createTable();
|
||||
|
||||
char * names[20] = { "piet", "gurbe", "siebe", "gerben", "twain", "piet", "gurbe", "siebe", "gerben", "twain", "piet", "gurbe", "siebe", "gerben", "twain" };
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
|
||||
array * usersInsertArray = new array();
|
||||
|
||||
for (int i = 0; i <10; ++i) //20000
|
||||
{
|
||||
|
||||
char * name = names[i];
|
||||
|
||||
struct user * userInstance = new user();
|
||||
|
||||
userInstance->id = i;
|
||||
|
||||
userInstance->username = name;
|
||||
|
||||
userInstance->userlevel = 3;
|
||||
|
||||
userInstance->hash = "12345";
|
||||
|
||||
usersInsertArray.add( userInstance );
|
||||
|
||||
//sql->addRow( userInstance );
|
||||
|
||||
}
|
||||
|
||||
|
||||
sql->addRows( usersInsertArray );
|
||||
|
||||
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("Multiply: %f seconds, rows: %i\n", (double)( toc - tic ) / CLOCKS_PER_SEC, 10000);
|
||||
|
||||
|
||||
|
||||
array * usersA = sql->fetchRows( "SELECT * FROM user" );
|
||||
|
||||
//user * firstUser = new user();
|
||||
|
||||
user * firstUser = usersA->get( 1 );
|
||||
|
||||
//firstUser->id =10;
|
||||
|
||||
firstUser->username = "updated";
|
||||
|
||||
firstUser->userlevel = 1111;
|
||||
|
||||
firstUser->hash = "new hash";
|
||||
|
||||
console->log( "??", firstUser, "Yeey console.log workswith , , " );
|
||||
|
||||
sql->update( firstUser );
|
||||
|
||||
printf("before log\n\n");
|
||||
|
||||
|
||||
|
||||
|
||||
array * users = sql->fetchRows( "SELECT * FROM user " );
|
||||
|
||||
int userCount = users->total;
|
||||
|
||||
|
||||
printf("added rows: %i\n", userCount);
|
||||
|
||||
console->createHorisontalLine();
|
||||
printf("\e[1m");
|
||||
|
||||
printf(" ");
|
||||
|
||||
printf("%-20s", "id" );
|
||||
|
||||
printf("%-30s", "username" );
|
||||
|
||||
printf("%-20s", "userLevel" );
|
||||
|
||||
printf("%-30s", "Hash");
|
||||
|
||||
printf("\e[m");
|
||||
|
||||
printf("\n");
|
||||
|
||||
console->createHorisontalLine();
|
||||
|
||||
printf("users: %i\n", userCount);
|
||||
|
||||
for (int i = 0; i < userCount; ++i)
|
||||
{
|
||||
|
||||
struct user * userInstance = users->get( i );
|
||||
|
||||
userInstance->id;
|
||||
|
||||
userInstance->username;
|
||||
|
||||
printf(" ");
|
||||
|
||||
printf("%-20i", userInstance->id);
|
||||
|
||||
printf("%-30s", userInstance->username);
|
||||
|
||||
printf("%-20i", userInstance->userlevel);
|
||||
|
||||
printf("%-30s", userInstance->hash);
|
||||
|
||||
printf( "\n" );//%-30s
|
||||
|
||||
}
|
||||
|
||||
sql->free();
|
||||
|
||||
console->createHorisontalLine();
|
||||
|
||||
}
|
||||
|
||||
void multiInherintence() {
|
||||
|
||||
|
||||
|
||||
struct inherit * pointer = new inherit();
|
||||
|
||||
//pointer->propertyFromC = 20;
|
||||
|
||||
|
||||
printf("pointer->propertyFromC: %i\n\n", pointer->propertyFromC );
|
||||
|
||||
pointer->methodFromC();
|
||||
|
||||
printf("\n\n\n\n");
|
||||
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
|
||||
struct inherit instance = new inherit();
|
||||
|
||||
instance.propertyFromA = i;
|
||||
|
||||
instance.propertyFromB = i;
|
||||
|
||||
instance.propertyFromC = i;
|
||||
|
||||
instance.methodFromC();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
//int main()
|
||||
//{
|
||||
|
||||
//startOpenGL();
|
||||
|
||||
/*
|
||||
return 1;
|
||||
|
||||
struct user * newUser = new user();
|
||||
|
||||
newUser->id = 1;
|
||||
|
||||
newUser->username = "peter";
|
||||
|
||||
newUser->userlevel = 2134;
|
||||
|
||||
newUser->hash = "#234234325";
|
||||
|
||||
|
||||
|
||||
struct array * addresses = newUser->addresses;
|
||||
|
||||
|
||||
address * someAddress = new address();
|
||||
|
||||
someAddress->street = "HiLane";
|
||||
|
||||
someAddress->number = 1234;
|
||||
|
||||
addresses->add( someAddress );
|
||||
|
||||
|
||||
|
||||
|
||||
address * otherAddress = new address();
|
||||
|
||||
otherAddress->street = "OtherLane";
|
||||
|
||||
otherAddress->number = 4567;
|
||||
|
||||
addresses->add( otherAddress );
|
||||
|
||||
// todo newUser->addresses->add( otherAddress );
|
||||
|
||||
|
||||
printf("adresses count: %i\n\n", addresses->length() );
|
||||
|
||||
|
||||
char * something = "this is from an char * ";
|
||||
|
||||
int somethingElse = 123;
|
||||
|
||||
|
||||
console.log( "Goedendag",
|
||||
123456,
|
||||
"en een andere text.",
|
||||
something,
|
||||
somethingElse,
|
||||
"something en something",
|
||||
"in native c",
|
||||
23456,
|
||||
newUser,
|
||||
"and some text again" ); //&(int[3]){ 1, 2, 1 }, 3,
|
||||
|
||||
|
||||
|
||||
|
||||
sqlite3Example();
|
||||
|
||||
textAndArraysExamples();
|
||||
|
||||
|
||||
multiInherintence();
|
||||
|
||||
//tools->createHorisontalLine();
|
||||
|
||||
//createHorisontalLine();
|
||||
|
||||
|
||||
printf("\n\n\n\n\n\n\n");
|
||||
|
||||
|
||||
int totalInteger = 0;
|
||||
|
||||
printf("This just works out of the box.. \n");
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
|
||||
int someInteger = 10;
|
||||
|
||||
int theNegative = someInteger->negative();
|
||||
|
||||
totalInteger += theNegative;
|
||||
|
||||
//printf("this int is negative: %i\n\n", someInteger->negative() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
char somechar = 56;
|
||||
|
||||
|
||||
char * directory2 = "./assets/";
|
||||
|
||||
fileSystem * filesystem = new fileSystem();
|
||||
|
||||
char * directory = "./assets/";
|
||||
|
||||
array * files = filesystem->readDir( directory );
|
||||
|
||||
int fileCount = files->length();
|
||||
|
||||
for ( int i = 0; i < fileCount; ++i )
|
||||
{
|
||||
|
||||
char * file = files->get( i );
|
||||
|
||||
char * path = directory->concatenate( file );
|
||||
|
||||
text * source = filesystem->readFile( path, "binary" );
|
||||
|
||||
|
||||
printf("filename: %s\n", path);
|
||||
|
||||
printf("source: %s\n\n\n", source->value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
array * arrayA = new array();
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < 1000000; ++i)
|
||||
{
|
||||
vector2 testVector = new vector2( i, i * 450 );
|
||||
|
||||
vector2 testVector2 = new vector2( 10, 450 );
|
||||
|
||||
testVector->add( &testVector2 );
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("count: %i\n", count);
|
||||
|
||||
|
||||
for ( int i = 0; i < 1; ++i )
|
||||
{
|
||||
|
||||
vector2 testVector = new vector2( 10, 450 );
|
||||
|
||||
|
||||
arrayA->add( &testVector );
|
||||
|
||||
arrayA->add( &testVector );
|
||||
|
||||
|
||||
//printf("number of items in array: %i \n ", arrayA->length() );
|
||||
|
||||
vector2 * returned = arrayA->get( i );
|
||||
|
||||
//printf("returned vector x: %i \n ", returned->x );
|
||||
|
||||
//printf("returned vector y: %i \n ", returned->y );
|
||||
|
||||
vector2 testVector3 = new vector2( 2, 3 );
|
||||
|
||||
arrayA->set( i, &testVector3 );
|
||||
|
||||
vector2 * secondReturn = arrayA->get( 0 );
|
||||
|
||||
//printf("returned vector x: %i \n ", secondReturn->x );
|
||||
|
||||
//printf("returned vector y: %i \n ", secondReturn->y );
|
||||
|
||||
returned->add( secondReturn );
|
||||
|
||||
printf( "returned vector x: %i \n ", returned->x );
|
||||
|
||||
printf("returned vector y: %i \n ", returned->y );
|
||||
|
||||
}
|
||||
|
||||
|
||||
for ( int i = 0; i < 1; ++i )
|
||||
{
|
||||
|
||||
vector2 a = new vector2( 10, 10 );
|
||||
|
||||
vector2 b = new vector2( 10, 10 );
|
||||
|
||||
a->add( &b );
|
||||
|
||||
printf( "a->x: %i, a->y: %i\n", a.x, a.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
triangle * a = new triangle();
|
||||
|
||||
triangle * b = new triangle();
|
||||
|
||||
|
||||
a->add( b );
|
||||
}
|
||||
|
||||
|
||||
vector2 * t = new vector2( 2,2 );
|
||||
|
||||
int totalLength = 0;
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
|
||||
t->x = 100;
|
||||
|
||||
vector2 b = new vector2( 123, 2 );
|
||||
|
||||
b.x = 50;
|
||||
|
||||
vector2 c = new vector2( 1, 12 );
|
||||
|
||||
|
||||
c.x = i;
|
||||
|
||||
t->add( &b );
|
||||
|
||||
b.add( &c );
|
||||
|
||||
c.add( &b );
|
||||
|
||||
printf("a, %i\n", t->x);
|
||||
|
||||
printf("b, %i\n", b.x);
|
||||
|
||||
printf("c, %i\n", c.x);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
operatorOverload();
|
||||
|
||||
//printf("%s", other);
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
/*
|
||||
void operatorOverload() {
|
||||
|
||||
text * textB = new text("ook mooi");
|
||||
|
||||
char * other = malloc(5000);
|
||||
|
||||
vector3 * newVector3 = new vector3();
|
||||
|
||||
newVector3->x = 2;
|
||||
|
||||
newVector3->y = 2;
|
||||
|
||||
newVector3->z = 3;
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
{
|
||||
|
||||
vector3 * vector3B = new vector3();
|
||||
|
||||
|
||||
vector3B->x = 2;
|
||||
|
||||
vector3B->y = 3;
|
||||
|
||||
vector3B->z = 4;
|
||||
|
||||
|
||||
vector3 * vector3C = new vector3();
|
||||
|
||||
|
||||
vector3C->x = 2;
|
||||
|
||||
vector3C->y = 3;
|
||||
|
||||
vector3C->z = 4;
|
||||
|
||||
newVector3 = vector3C + ( vector3C + newVector3 + ( ( ( ( vector3B + vector3C ) ) + vector3C ) + vector3B ) );
|
||||
|
||||
newVector3 = newVector3 + ( ( vector3B + vector3C ) + vector3C );
|
||||
|
||||
}
|
||||
|
||||
console->log( newVector3 );
|
||||
|
||||
text * textA = new text("mooi");
|
||||
|
||||
if( textA == textB ) {
|
||||
|
||||
printf("mooi zo");
|
||||
|
||||
}
|
||||
|
||||
text * textC = new text("mooi");
|
||||
|
||||
text * textD = new text("mooi");
|
||||
|
||||
|
||||
if( ( ( textA == textC ) && ( textA == textD ) ) && textA == textD ) {
|
||||
|
||||
printf("Good, This works.\n\n");
|
||||
|
||||
}
|
||||
|
||||
char * realChar = "something";
|
||||
|
||||
if( "something" == realChar ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( realChar == "something" ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( realChar == realChar ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( "something" == "something" && realChar == "something" ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
char * fancy = "aaa ";
|
||||
|
||||
char * aaaa = " bbb ";
|
||||
|
||||
char * bbbb = " ccc ";
|
||||
|
||||
char * dddd = " dddd ";
|
||||
|
||||
other += ( fancy + ( bbbb + aaaa ) + dddd ) + " eee" + " het is een wonder ";
|
||||
|
||||
other += ("boven wonder" + returnText("this is a normal function") + " and this " + ( "works just good..." + dddd ));
|
||||
|
||||
other += "something" + ( returnText("this is a normal function") + textB->toNative() + textB->value );
|
||||
|
||||
other += ("and here some text") + (textB->toNative()) ;
|
||||
|
||||
other += ( textB->value + ( ( textB->value ) + returnText("this is a normal function") ) + (textB->toNative() + "here some text" + textB->value ) );
|
||||
|
||||
other += textB->value + textB->value + ( textB->value + textB->value ) + ( textB->value + textB->value ) + textB->value + textB->value;
|
||||
|
||||
console.log( "Mooi zo ", other );
|
||||
|
||||
|
||||
multiThread();
|
||||
|
||||
vector3 * VectorTester = new vector3();
|
||||
*/
|
||||
//}
|
||||
|
||||
|
||||
char * returnText( char * text ) {
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
205
application/source/array.c
Executable file
205
application/source/array.c
Executable file
@@ -0,0 +1,205 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <char.h>
|
||||
|
||||
#include <text.h>
|
||||
|
||||
|
||||
class array{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
void * * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void * * data() {
|
||||
|
||||
return this->items;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void * get( int index )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
void set( int index, void * item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
void * * items = realloc( this->items, sizeof( void * ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void add( void * item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
char * join( char * separator ) {
|
||||
|
||||
int count = this->length();
|
||||
|
||||
text * result = new text( "" );
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
char * currentPart = this->items[ i ];
|
||||
|
||||
if( i > 0 ) {
|
||||
|
||||
result->append( separator );
|
||||
|
||||
}
|
||||
|
||||
result->append( currentPart );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return result->value;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this->items[index] = NULL;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = NULL;
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int array_push( void * item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( void * item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
void * pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
void * lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
bool includes( char * value ) {
|
||||
|
||||
int count = this->length();
|
||||
|
||||
for ( int index = 0; index < count; ++index )
|
||||
{
|
||||
|
||||
char * currentText = this->get(index);
|
||||
|
||||
//printf("test: %s \n", this->items[index]);
|
||||
|
||||
if( currentText == value ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
178
application/source/arrayText.c
Normal file
178
application/source/arrayText.c
Normal file
@@ -0,0 +1,178 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
|
||||
class arrayText{
|
||||
|
||||
int capacity = 40;
|
||||
|
||||
int total = 0;
|
||||
|
||||
char * * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
bool includes( char * value ) {
|
||||
|
||||
int count = this->length();
|
||||
|
||||
for ( int index = 0; index < count; ++index )
|
||||
{
|
||||
|
||||
char * currentText = this->get(index);
|
||||
|
||||
//printf("test: %s \n", this->items[index]);
|
||||
|
||||
if( currentText == value ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
char * get( int index )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
return "NULL";
|
||||
|
||||
}
|
||||
|
||||
void set( int index, char * item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
char * * items = realloc( this->items, sizeof( char * ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void add( char * item )
|
||||
{
|
||||
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
this->items[ this->total ] = ( char * ) malloc( 200 * sizeof( char ) );
|
||||
|
||||
strcpy(this->items[ this->total ], item);
|
||||
|
||||
|
||||
this->total++;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
//this->items[index] = "";
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = "";
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unsigned int array_push( char * item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( char * item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
char * pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
char * lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
1
application/source/assets/test
Executable file
1
application/source/assets/test
Executable file
@@ -0,0 +1 @@
|
||||
this is just a test.
|
||||
75
application/source/cache.c
Normal file
75
application/source/cache.c
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "text.h"
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#include "fileSystem.h"
|
||||
|
||||
|
||||
class cache {
|
||||
|
||||
struct array * files = new array();
|
||||
|
||||
text * getFile( char * filePath ) {
|
||||
|
||||
struct file * currentFile = this->getCachedFileByPath( filePath );
|
||||
|
||||
if( currentFile == NULL ) {
|
||||
|
||||
//printf("reading new file: %s\n\n", filePath);
|
||||
|
||||
struct text * content = filesystem->readFile( filePath, "binary" );
|
||||
|
||||
this->addFile( filePath, content );
|
||||
|
||||
return content;
|
||||
|
||||
} else {
|
||||
|
||||
return currentFile->content;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
addFile( char * filePath, struct text * content ) {
|
||||
|
||||
struct file * newFile = new file();
|
||||
|
||||
newFile->filePath = filePath;
|
||||
|
||||
newFile->content = content;
|
||||
|
||||
this->files->add( newFile );
|
||||
|
||||
}
|
||||
|
||||
struct file * getCachedFileByPath( char * filePath ) {
|
||||
|
||||
struct array * files = this->files;
|
||||
|
||||
int count = files->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
file * currentFile = files->get( i );
|
||||
|
||||
if( currentFile->filePath == filePath ) {
|
||||
|
||||
//printf("using cached file: %s\n\n", filePath);
|
||||
|
||||
return currentFile;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
60
application/source/callbacks.c
Normal file
60
application/source/callbacks.c
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class someClass{
|
||||
|
||||
char * value = "someValue";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class callback{
|
||||
|
||||
constructor() {
|
||||
|
||||
this->testCallbacks();
|
||||
|
||||
}
|
||||
|
||||
void callCallback( void ( * function )( struct someClass * caller, int ) , int number, struct someClass * caller ) {
|
||||
|
||||
function( caller, number );
|
||||
|
||||
}
|
||||
|
||||
void printTest( struct someClass * caller, int a ) {
|
||||
|
||||
printf("print test: %i %s .\n\n\n\n", a, caller->value);
|
||||
|
||||
}
|
||||
|
||||
void anotherPrintTest( struct someClass * caller, int a ) {
|
||||
|
||||
//struct user * someUser = someObject;
|
||||
|
||||
printf("another print test: %i %s .\n\n\n\n", a, caller->value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void testCallbacks() {
|
||||
|
||||
someClass * instance = new someClass();
|
||||
|
||||
this->callCallback( this->printTest, 1234, instance );
|
||||
|
||||
this->callCallback( this->printTest, 5678, instance );
|
||||
|
||||
this->callCallback( this->printTest, 910, instance );
|
||||
|
||||
this->callCallback( this->anotherPrintTest, 910, instance );
|
||||
|
||||
this->callCallback( this->anotherPrintTest, 910, instance );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
171
application/source/char.c
Executable file
171
application/source/char.c
Executable file
@@ -0,0 +1,171 @@
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <array.h>
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
class char{
|
||||
|
||||
|
||||
int operator==( char * b ) {
|
||||
|
||||
return this->compare( b );
|
||||
|
||||
}
|
||||
|
||||
void operator+=( char * b ) {
|
||||
|
||||
// not proper
|
||||
strcat( this, b );
|
||||
|
||||
}
|
||||
|
||||
char * operator+( char * b ) {
|
||||
|
||||
return this->concatenate( b );
|
||||
|
||||
}
|
||||
|
||||
int compare( char * b ) {
|
||||
|
||||
return strcmp( this, b ) == 0;
|
||||
|
||||
}
|
||||
|
||||
char * concatenate( char * b ) {
|
||||
|
||||
int lengthA = strlen( this );
|
||||
|
||||
int lengthB = strlen( b );
|
||||
|
||||
char * pointer = this;
|
||||
|
||||
char * copy = ( char * ) malloc( ( lengthA + lengthB + 1 ) * sizeof( char ) );
|
||||
|
||||
int idx = 0;
|
||||
|
||||
while ( * pointer != '\0' ){
|
||||
|
||||
copy[idx++] = *pointer++;
|
||||
|
||||
}
|
||||
|
||||
pointer = &b[0];
|
||||
|
||||
while ( * pointer != '\0' ){
|
||||
|
||||
copy[idx++] = *pointer++;
|
||||
|
||||
}
|
||||
|
||||
copy[ idx++ ] = '\0';
|
||||
|
||||
return ©[0];
|
||||
|
||||
}
|
||||
|
||||
int includes( char * compare ) {
|
||||
|
||||
if ( strstr( this, compare ) != NULL ) {
|
||||
|
||||
return 1;
|
||||
|
||||
} else {
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
char * clone( ) {
|
||||
|
||||
char * newCopy = malloc( sizeof( char ) * strlen( this ) );
|
||||
|
||||
strcpy( newCopy, this );
|
||||
|
||||
return newCopy;
|
||||
|
||||
}
|
||||
|
||||
char * copy( ) {
|
||||
|
||||
char * newCopy = malloc( sizeof( char ) * strlen( this ) );
|
||||
|
||||
strcpy( newCopy, this );
|
||||
|
||||
return newCopy;
|
||||
|
||||
}
|
||||
|
||||
struct array * split( char * needle )
|
||||
{
|
||||
|
||||
char * haystack = this->clone();
|
||||
|
||||
struct array * keys = new array();
|
||||
|
||||
int count = 0;
|
||||
|
||||
char * tmp = haystack;
|
||||
|
||||
char * token = strtok( haystack, needle );
|
||||
|
||||
int i = 0;
|
||||
|
||||
while ( token ) {
|
||||
|
||||
array_add( keys, token );
|
||||
|
||||
//printf("strtok: %s \n", token );
|
||||
|
||||
token = (char *) strtok( NULL, needle );
|
||||
|
||||
}
|
||||
|
||||
return keys;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
char * removeWhitespaceLeft()
|
||||
{
|
||||
char * s = this;
|
||||
|
||||
while(isspace(*s)) s++;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
char * removeWhitespaceRight()
|
||||
{
|
||||
char * s = this;
|
||||
|
||||
char* back = s + strlen(s);
|
||||
|
||||
while( isspace( *--back ) );
|
||||
|
||||
*( back + 1 ) = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
char * removeWhitespace( )
|
||||
{
|
||||
|
||||
return this->removeWhitespaceLeft()->removeWhitespaceRight();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
264
application/source/console.c
Normal file
264
application/source/console.c
Normal file
@@ -0,0 +1,264 @@
|
||||
|
||||
#include <classConfiguration.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
#define isCompatible(x, type) _Generic(x, type: true, default: false)
|
||||
|
||||
|
||||
extern struct consoleManager * console;
|
||||
|
||||
class consoleManager{
|
||||
|
||||
char * whiteSpace( int whiteSpaceCount ) {
|
||||
|
||||
char * output = malloc( whiteSpaceCount + 1 );
|
||||
|
||||
for (int i = 0; i < whiteSpaceCount; ++i)
|
||||
{
|
||||
|
||||
strcat( output, " " );
|
||||
|
||||
}
|
||||
|
||||
output[whiteSpaceCount] = 0;
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
logObject( void * voidPointer, int classIndex, int level ) {
|
||||
|
||||
|
||||
char * whiteSpace = this->whiteSpace( level );
|
||||
|
||||
level++;
|
||||
|
||||
char * className = getClassName( classIndex );
|
||||
|
||||
printf( "\n\n" );
|
||||
|
||||
printf( whiteSpace );
|
||||
|
||||
printf(" %s : {\n", className );
|
||||
|
||||
|
||||
|
||||
char * * propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
char * pointer = voidPointer;
|
||||
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
printf( whiteSpace );
|
||||
|
||||
printf(" %-20s : ", propertyName);
|
||||
|
||||
int propertyDatatypeIndex = getPropertyDatatypeIndex( datatypeIndices, propertyIndex );
|
||||
|
||||
int propertyOffset = getPropertyOffsetByPropertyIndex( propertyOffsets, propertyIndex );
|
||||
|
||||
|
||||
if( propertyDatatypeIndex == -5 ) {
|
||||
|
||||
int value = *( int * )(pointer + propertyOffset);
|
||||
|
||||
printf( whiteSpace );
|
||||
|
||||
printf("%-20i ", value );
|
||||
|
||||
|
||||
|
||||
} else if( propertyDatatypeIndex == -3 ) {
|
||||
|
||||
//char * columnValueCopy = ( char * ) malloc( 8 );
|
||||
|
||||
uintptr_t * value = ( uintptr_t * ) ( pointer + propertyOffset );
|
||||
|
||||
//strncpy( &columnValueCopy, value, 8 );
|
||||
|
||||
printf( whiteSpace );
|
||||
|
||||
printf( "%-20s", ( char * ) * value );
|
||||
|
||||
} else if( propertyDatatypeIndex > 0 ) {
|
||||
|
||||
char * memberClassName = getClassName( propertyDatatypeIndex );
|
||||
|
||||
if( strcmp( memberClassName, "array" ) == 0 ) {
|
||||
|
||||
struct array * memberArray = *(struct array ** )(pointer + propertyOffset) ;
|
||||
|
||||
if( memberArray == NULL ) {
|
||||
|
||||
printf(" this has to be fixed, array is not created.\n");
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
int numberRows = memberArray->length();
|
||||
|
||||
int * arrayPointer = ( int * ) memberArray->items;
|
||||
|
||||
for (int k = 0; k < numberRows; ++k)
|
||||
{
|
||||
|
||||
void * pointer = memberArray->get( k );
|
||||
|
||||
short * row = (short*)(pointer);
|
||||
|
||||
this->logObject( pointer, (int) *row, level );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//printf("This is another class: %s\n\n", memberClassName);
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
printf( whiteSpace );
|
||||
|
||||
printf( " }\n" );
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void log( ... ) {
|
||||
|
||||
//int datatypes[], int count,
|
||||
|
||||
int level = 0;
|
||||
|
||||
va_list args;
|
||||
|
||||
va_start( args, count );
|
||||
|
||||
//printf("count: %i\n\n", count);
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
int datatype = datatypes[i]; // va_arg_type( argumentIndex )
|
||||
|
||||
//printf("datatype %i\n", datatype);
|
||||
|
||||
if( datatype == -2 ) {
|
||||
|
||||
char * message = va_arg( args, char * );
|
||||
|
||||
printf("%s", message);
|
||||
|
||||
}
|
||||
|
||||
if( datatype == -1 ) {
|
||||
|
||||
int message = va_arg( args, int );
|
||||
|
||||
printf("%i", message);
|
||||
}
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
//int classIndex = va_arg( args, int );
|
||||
|
||||
void * voidPointer = va_arg( args, void * );
|
||||
|
||||
this->logObject( voidPointer, datatype, level++ );
|
||||
|
||||
|
||||
}
|
||||
|
||||
printf(" ");
|
||||
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
va_end( args);
|
||||
|
||||
}
|
||||
|
||||
void error( char * message ) {
|
||||
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_DIM_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
#define ANSI_COLOR_BRIGHT_YELLOW "\x1b[93m"
|
||||
|
||||
printf( ANSI_COLOR_RED );
|
||||
|
||||
printf( "\n\n Error: " );
|
||||
|
||||
printf( "%s\n\n", message );
|
||||
|
||||
printf( ANSI_COLOR_RESET );
|
||||
|
||||
exit( 0 );
|
||||
|
||||
}
|
||||
|
||||
void createHorisontalLine() {
|
||||
|
||||
struct winsize w;
|
||||
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
|
||||
for (int i = 0; i < w.ws_col; ++i)
|
||||
{
|
||||
|
||||
printf("-");
|
||||
|
||||
};
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
3
application/source/define.c
Normal file
3
application/source/define.c
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
#define someImportantDefine
|
||||
138
application/source/engine/arrayUnsignedInteger.c
Normal file
138
application/source/engine/arrayUnsignedInteger.c
Normal file
@@ -0,0 +1,138 @@
|
||||
|
||||
|
||||
class arrayUnsignedInteger{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
unsigned int * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
unsigned int get( int index )
|
||||
{
|
||||
|
||||
|
||||
return this->items[index];
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void set( int index, unsigned int item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
int * items = realloc( this->items, sizeof( unsigned int ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void add( unsigned int item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this->items[index] = 0.0;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = 0.0;
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unsigned int array_push( unsigned int item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( int item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
unsigned int pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
int lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
560
application/source/engine/block.c
Normal file
560
application/source/engine/block.c
Normal file
@@ -0,0 +1,560 @@
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fileSystem.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
#include "sampler2D.h"
|
||||
|
||||
#include "member.h"
|
||||
|
||||
class block{
|
||||
|
||||
array * members = new array();
|
||||
|
||||
GLint buffer;
|
||||
|
||||
GLuint index;
|
||||
|
||||
GLenum bufferType;
|
||||
|
||||
GLint bufferSize;
|
||||
|
||||
GLint bindingPoint;
|
||||
|
||||
GLchar * name;
|
||||
|
||||
float * data;
|
||||
|
||||
GLint autoUpload = 0;
|
||||
|
||||
|
||||
|
||||
add( struct member * memberInstance ) {
|
||||
|
||||
this->members->add( memberInstance );
|
||||
|
||||
}
|
||||
|
||||
enableAutoUpload() {
|
||||
|
||||
this->autoUpload = 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char * getBufferTypeText( ) {
|
||||
|
||||
switch( this->bufferType ) {
|
||||
|
||||
case GL_SHADER_STORAGE_BUFFER:
|
||||
|
||||
return "GL_SHADER_STORAGE_BUFFER";
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNIFORM_BUFFER:
|
||||
|
||||
return "GL_UNIFORM_BUFFER";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return "buffer message not found";
|
||||
|
||||
}
|
||||
|
||||
createBuffer() {
|
||||
|
||||
unsigned int blockBufferSize = this->bufferSize; // allocate 152 bytes of memory
|
||||
|
||||
//printf(" create array for block %s os size %i \n", this->name, this->bufferSize );
|
||||
|
||||
this->data = ( float * ) malloc( blockBufferSize );
|
||||
|
||||
glGenBuffers( 1, &this->buffer );
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->index, this->buffer );
|
||||
|
||||
glBufferData( this->bufferType, blockBufferSize, 0, GL_DYNAMIC_DRAW );
|
||||
|
||||
}
|
||||
|
||||
void upload() {
|
||||
|
||||
//printf("upload uniform buffer: %f %f %f %f\n", this->data[0], this->data[1], this->data[2], this->data[3]);
|
||||
|
||||
//printf("upload uniform buffer: %i\n", this->index);
|
||||
|
||||
//glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
//glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
//glBufferData( this->bufferType, this->bufferSize, this->data, GL_DYNAMIC_DRAW );
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, 0, this->bufferSize, this->data );
|
||||
|
||||
}
|
||||
|
||||
mapBufferError( void * pointer ) {
|
||||
|
||||
if ( pointer == NULL ){
|
||||
|
||||
GLenum errorCode = glGetError();
|
||||
|
||||
switch( errorCode ) {
|
||||
|
||||
case GL_INVALID_ENUM:
|
||||
|
||||
printf("GL_INVALID_ENUM\n");
|
||||
|
||||
break;
|
||||
|
||||
case GL_INVALID_OPERATION:
|
||||
|
||||
printf("GL_INVALID_OPERATION\n");
|
||||
|
||||
break;
|
||||
|
||||
case GL_INVALID_VALUE:
|
||||
|
||||
printf("GL_INVALID_VALUE\n");
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("null pointer on buffer: %i\n", errorCode);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void * getMemberArray( char * name ) {
|
||||
|
||||
glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT );
|
||||
|
||||
vector<vector2> * output = new vector();
|
||||
|
||||
|
||||
int uniformCount = this->members->length();
|
||||
|
||||
for (int i = 0; i < uniformCount; ++i)
|
||||
{
|
||||
member * currentMember = this->members->get( i );
|
||||
|
||||
char * memberName = ( char * ) currentMember->name;
|
||||
|
||||
if( memberName == name ) {
|
||||
|
||||
int size = currentMember->size * 8;
|
||||
|
||||
int offset = currentMember->offset / 4;
|
||||
|
||||
output->items = glMapBufferRange( GL_SHADER_STORAGE_BUFFER, 0, size, GL_MAP_WRITE_BIT );
|
||||
|
||||
output->total = currentMember->size;
|
||||
|
||||
this->mapBufferError( output->items );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
glUnmapBuffer( GL_SHADER_STORAGE_BUFFER );
|
||||
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
void setMemberArrayRow( char * name, int arrayIndex, float * data ) {
|
||||
|
||||
int memberCount = this->members->length();
|
||||
|
||||
for (int i = 0; i < memberCount; ++i)
|
||||
{
|
||||
member * currentMember = this->members->get( i );
|
||||
|
||||
char * memberName = ( char * ) currentMember->name;
|
||||
|
||||
if( memberName == name ) {
|
||||
|
||||
int size = currentMember->size * 8;
|
||||
|
||||
int arrayStride = currentMember->arrayStride;
|
||||
|
||||
int offset = arrayStride * arrayIndex;
|
||||
|
||||
//printf("set array stride: %i %i\n", arrayIndex, arrayStride);
|
||||
|
||||
//int offset = currentMember->offset / 4;
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, offset, arrayStride, data );
|
||||
|
||||
}
|
||||
|
||||
|
||||
memcpy( this->data + offset, data, arrayStride );
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setMemberArray( char * name, float * data ) {
|
||||
|
||||
int memberCount = this->members->length();
|
||||
|
||||
for (int i = 0; i < memberCount; ++i)
|
||||
{
|
||||
member * currentMember = this->members->get( i );
|
||||
|
||||
char * memberName = ( char * ) currentMember->name;
|
||||
|
||||
if( memberName == name ) {
|
||||
|
||||
int size = currentMember->size * 8;
|
||||
|
||||
int offset = currentMember->offset / 4;
|
||||
|
||||
memcpy( this->data + offset, data, size );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setMemberItem( char * name, int index, void * value ) {
|
||||
|
||||
|
||||
int uniformCount = this->members->length();
|
||||
|
||||
//printf("uniformCount: %i\n\n", uniformCount);
|
||||
|
||||
for (int i = 0; i < uniformCount; ++i)
|
||||
{
|
||||
member * currentMember = this->members->get( i );
|
||||
|
||||
char * memberName = ( char * ) currentMember->name;
|
||||
|
||||
if( memberName == name ) {
|
||||
|
||||
//printf("\n\n Update this uniform from uniform block %s\n\n", name);
|
||||
int stride;
|
||||
|
||||
int strideNormalized;
|
||||
|
||||
int size;
|
||||
|
||||
int offset;
|
||||
|
||||
|
||||
switch( currentMember->type ) {
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
stride = currentMember->arrayStride;
|
||||
|
||||
strideNormalized = stride / sizeof( float ) ;
|
||||
|
||||
//printf("get size of item: %i\n",strideNormalized );
|
||||
|
||||
|
||||
|
||||
vector2 * vector2Value = ( vector2 * ) value;
|
||||
|
||||
size = 8;
|
||||
|
||||
offset = currentMember->offset;
|
||||
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
GLint arrayIndex = ( strideNormalized * index * 2 * 8 );
|
||||
|
||||
float data[2] = { vector2Value->x, vector2Value->y };
|
||||
|
||||
printf("%i: (size:%i) %f %f\n", arrayIndex, data[0], data[1]);
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, arrayIndex, size, data );
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
GLint arrayIndex = ( strideNormalized * index );
|
||||
|
||||
// need to fix this
|
||||
if( currentMember->topLevelSize == 1 ) {
|
||||
|
||||
//arrayIndex = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//printf(" %i: %f \n", dataOffset + 1 + arrayIndex , vector2Value->y );
|
||||
|
||||
this->data[ dataOffset + arrayIndex ] = vector2Value->x;
|
||||
|
||||
this->data[ dataOffset + 1 + arrayIndex ] = vector2Value->y;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
stride = currentMember->arrayStride;
|
||||
|
||||
strideNormalized = stride / sizeof( float ) ;
|
||||
|
||||
|
||||
vector3 * vector3Value = ( vector3 * ) value;
|
||||
|
||||
size = 12;
|
||||
|
||||
offset = currentMember->offset;
|
||||
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
float data[3] = { vector3Value->x, vector3Value->y, vector3Value->z };
|
||||
|
||||
// printf("%f %f\n", data[0], data[1]);
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, offset, size, data );
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
GLint arrayIndex = ( strideNormalized * index );
|
||||
|
||||
this->data[ dataOffset + arrayIndex ] = vector3Value->x;
|
||||
|
||||
this->data[ dataOffset + 1 + arrayIndex ] = vector3Value->y;
|
||||
|
||||
this->data[ dataOffset + 2 + arrayIndex ] = vector3Value->z;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GL_SAMPLER_2D:
|
||||
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setData( float * data ) {
|
||||
|
||||
this->data = data;
|
||||
|
||||
}
|
||||
|
||||
void setMember( char * name, void * value ) {
|
||||
|
||||
int uniformCount = this->members->length();
|
||||
|
||||
//printf("uniformCount: %i\n\n", uniformCount);
|
||||
|
||||
for (int i = 0; i < uniformCount; ++i)
|
||||
{
|
||||
member * currentMember = this->members->get( i );
|
||||
|
||||
char * memberName = ( char * ) currentMember->name;
|
||||
|
||||
if( memberName == name ) {
|
||||
|
||||
//printf("\n\n Update this uniform from uniform block %s\n\n", name);
|
||||
|
||||
GLuint size = 8;
|
||||
|
||||
GLint offset = currentMember->offset;
|
||||
|
||||
switch( currentMember->type ) {
|
||||
|
||||
case GL_INT:
|
||||
|
||||
int intValue = *( int * ) value;
|
||||
|
||||
printf("set int value: %i offset: %i\n", intValue, offset);
|
||||
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
float data[1] = { value };
|
||||
|
||||
// printf("%f %f\n", data[0], data[1]);
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, offset, size, data );
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
this->data[ dataOffset ] = intValue;
|
||||
|
||||
//this->data[ dataOffset + 1 ] = vector2Value->y;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT:
|
||||
|
||||
float floatValue = *( float * ) value;
|
||||
|
||||
printf("set int value: %f offset: %i\n", floatValue, offset);
|
||||
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
float data[1] = { value };
|
||||
|
||||
// printf("%f %f\n", data[0], data[1]);
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, offset, size, data );
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
this->data[ dataOffset ] = floatValue;
|
||||
|
||||
//this->data[ dataOffset + 1 ] = vector2Value->y;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
vector2 * vector2Value = ( vector2 * ) value;
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
float data[2] = { vector2Value->x, vector2Value->y };
|
||||
|
||||
|
||||
glBindBuffer( this->bufferType, this->buffer );
|
||||
|
||||
glBindBufferBase( this->bufferType, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferSubData( this->bufferType, offset, size, data );
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
this->data[ dataOffset ] = vector2Value->x;
|
||||
|
||||
this->data[ dataOffset + 1 ] = vector2Value->y;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
vector3 * vector3Value = ( vector3 * ) value;
|
||||
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case GL_SAMPLER_2D:
|
||||
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
302
application/source/engine/element.c
Normal file
302
application/source/engine/element.c
Normal file
@@ -0,0 +1,302 @@
|
||||
|
||||
#include "vector2.h"
|
||||
|
||||
#include "vector3.h"
|
||||
|
||||
#include "vector4.h"
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
#include "classConfiguration.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
||||
class element{
|
||||
|
||||
int index;
|
||||
|
||||
vector3 position = new vector3( 0, 0, 0 );
|
||||
|
||||
vector2 size = new vector2( 0, 0 );
|
||||
|
||||
vector3 backgroundColor = new vector3( 0, 0, 0 );
|
||||
|
||||
|
||||
|
||||
vector3 originalPosition = new vector3( 0, 0, 0 );
|
||||
|
||||
vector2 originalSize = new vector2( 0, 0 );
|
||||
|
||||
vector3 originalBackgroundColor = new vector3( 0, 0, 0 );
|
||||
|
||||
|
||||
|
||||
float opacity = 1;
|
||||
|
||||
char * backgroundImagePath = "";
|
||||
|
||||
bool useBackgroundImage = false;
|
||||
|
||||
int features = 0;
|
||||
|
||||
vector<char *> * featureNames = new vector();
|
||||
|
||||
|
||||
constructor() {
|
||||
|
||||
this->featureNames->add( "useBackgroundImage" );
|
||||
|
||||
this->featureNames->add( "useBorder" );
|
||||
|
||||
}
|
||||
|
||||
|
||||
vector4 colorConverter( char * hexValue )
|
||||
{
|
||||
|
||||
int r;
|
||||
|
||||
int g;
|
||||
|
||||
int b;
|
||||
|
||||
int a;
|
||||
|
||||
sscanf( hexValue, "%02x%02x%02x%02x", &r, &g, &b, &a );
|
||||
|
||||
printf("opacity: %i\n");
|
||||
|
||||
struct vector4 rgbColor = new vector4( r, g, b, (float)a );
|
||||
|
||||
return rgbColor;
|
||||
}
|
||||
|
||||
|
||||
int textureIndex;
|
||||
|
||||
void * background;
|
||||
|
||||
setOriginal() {
|
||||
|
||||
this->originalSize = this->size;
|
||||
|
||||
this->originalPosition = this->position;
|
||||
|
||||
this->originalBackgroundColor = this->backgroundColor;
|
||||
|
||||
}
|
||||
|
||||
set size( vector2 a ) {
|
||||
|
||||
this->size.x = a.x;
|
||||
|
||||
this->size.y = a.y;
|
||||
|
||||
printf("set size: %f %f\n", this->size.x, this->size.y);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
set position( vector3 a ) {
|
||||
|
||||
this->position.x = a.x;
|
||||
|
||||
this->position.y = a.y;
|
||||
|
||||
this->position.z = a.z;
|
||||
|
||||
printf("set position: %f %f\n", this->position.x, this->position.y);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool featureIsEnabled( int featureValue ) {
|
||||
|
||||
return ( this->features & featureValue ) > 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int getFeatureValueByFeatureName( char * featureName ) {
|
||||
|
||||
int count = this->featureNames->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
char * currentFeatureName = this->featureNames->get( i );
|
||||
|
||||
if( featureName == currentFeatureName ) {
|
||||
|
||||
return powf( 2, i );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int updateFeature() {
|
||||
|
||||
int currentFeatureValue;
|
||||
|
||||
currentFeatureValue = this->getFeatureValueByFeatureName( "useBackgroundImage" );
|
||||
|
||||
if( this->featureIsEnabled( currentFeatureValue ) ) {
|
||||
|
||||
if( !this->useBackgroundImage ) {
|
||||
|
||||
this->features -= currentFeatureValue;
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if( this->useBackgroundImage ) {
|
||||
|
||||
this->features += currentFeatureValue;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return this->features;
|
||||
|
||||
//featureIsEnabled( int featureValue )
|
||||
|
||||
|
||||
}
|
||||
|
||||
set background( ... ) {
|
||||
|
||||
va_list args;
|
||||
|
||||
va_start( args, count );
|
||||
|
||||
int datatype = datatypes[0];
|
||||
|
||||
|
||||
printf("datatype: %i\n\n\n", datatype);
|
||||
|
||||
if( datatype == -2 ) {
|
||||
|
||||
char * message = va_arg( args, char * );
|
||||
|
||||
if( ( char ) message[0] == 35 ) {
|
||||
|
||||
printf("Hex color\n\n\n");
|
||||
|
||||
message++;
|
||||
|
||||
vector4 rgba = this->colorConverter( message );
|
||||
|
||||
this->backgroundColor = new vector3( rgba.x, rgba.y, rgba.z );
|
||||
|
||||
this->opacity = rgba.w / 256;
|
||||
|
||||
this->useBackgroundImage = false;
|
||||
|
||||
} else {
|
||||
|
||||
this->backgroundImagePath = message;
|
||||
|
||||
this->useBackgroundImage = true;
|
||||
|
||||
printf("path\n\n\n");
|
||||
|
||||
}
|
||||
|
||||
printf(" char *: %s\n\n\n ", message);
|
||||
|
||||
}
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
char * className = getClassName( datatype );
|
||||
|
||||
if( className == "vector3" ) {
|
||||
|
||||
vector3 message = va_arg( args, vector3 );
|
||||
|
||||
this->backgroundColor = message;
|
||||
|
||||
this->useBackgroundImage = false;
|
||||
|
||||
}
|
||||
|
||||
if( className == "vector4" ) {
|
||||
|
||||
vector4 message = va_arg( args, vector4 );
|
||||
|
||||
this->backgroundColor = new vector3( message.x, message.y, message.z );
|
||||
|
||||
this->opacity = message.w;
|
||||
|
||||
this->useBackgroundImage = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//printf(" %f %f %f %f \n\n\n ", this->backgroundColor.x, this->backgroundColor.y, this->backgroundColor.z, this->opacity);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
click() {
|
||||
|
||||
|
||||
this->backgroundColor = new vector3( 0, 256, 256 );
|
||||
|
||||
}
|
||||
|
||||
mousedown() {
|
||||
|
||||
//printf("executed click event\n\n");
|
||||
|
||||
this->backgroundColor = new vector3( 0, 256, 256 );
|
||||
|
||||
this->position = new vector3( 256, 256, 100 );
|
||||
|
||||
}
|
||||
|
||||
mouseup() {
|
||||
|
||||
//printf("executed click event\n\n");
|
||||
|
||||
this->backgroundColor = this->originalBackgroundColor;
|
||||
|
||||
this->position = this->originalPosition;
|
||||
|
||||
}
|
||||
|
||||
mouseover() {
|
||||
|
||||
this->backgroundColor = new vector3( 256, 256, 256 );
|
||||
|
||||
}
|
||||
|
||||
mouseleave() {
|
||||
|
||||
printf("mouse leave\n");
|
||||
|
||||
this->backgroundColor = this->originalBackgroundColor;
|
||||
|
||||
}
|
||||
}
|
||||
100
application/source/engine/event.c
Normal file
100
application/source/engine/event.c
Normal file
@@ -0,0 +1,100 @@
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
|
||||
/*
|
||||
|
||||
DOM Level 3 defines nine mouse events.
|
||||
|
||||
event
|
||||
|
||||
keyboard
|
||||
|
||||
key : ArrowUp
|
||||
|
||||
keyCode : 123
|
||||
|
||||
shiftKey : false
|
||||
|
||||
ctrlKey : true
|
||||
|
||||
altKey : false
|
||||
|
||||
metaKey : false
|
||||
|
||||
|
||||
screen
|
||||
|
||||
position
|
||||
|
||||
vector2
|
||||
|
||||
size
|
||||
|
||||
vector2
|
||||
|
||||
|
||||
mouse
|
||||
|
||||
position
|
||||
|
||||
vector2
|
||||
|
||||
button:
|
||||
0 Left
|
||||
|
||||
1 middle
|
||||
|
||||
2 right
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
class mouse{
|
||||
|
||||
vector2 position;
|
||||
|
||||
int button;
|
||||
|
||||
vector<char *> * eventTypes = new vector();
|
||||
|
||||
}
|
||||
|
||||
class screen{
|
||||
|
||||
vector2 size;
|
||||
|
||||
vector2 position;
|
||||
|
||||
}
|
||||
|
||||
class keyboard{
|
||||
|
||||
int keyCode;
|
||||
|
||||
bool shiftKey;
|
||||
|
||||
bool ctrlKey;
|
||||
|
||||
bool altKey;
|
||||
|
||||
bool metaKey;
|
||||
|
||||
}
|
||||
|
||||
class event{
|
||||
|
||||
struct mouse * mouse = new mouse();
|
||||
|
||||
struct screen * screen = new screen();
|
||||
|
||||
struct keyboard * keyboard = new keyboard();
|
||||
|
||||
}
|
||||
290
application/source/engine/eventManager.c
Normal file
290
application/source/engine/eventManager.c
Normal file
@@ -0,0 +1,290 @@
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <event.h>
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <vector.h>
|
||||
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
|
||||
|
||||
class eventManger{
|
||||
|
||||
Display * mainDisplay;
|
||||
|
||||
Window mainWindow;
|
||||
|
||||
Window RootWindow;
|
||||
|
||||
event * lastEvent;
|
||||
|
||||
clock_t lastMouseDownTime;
|
||||
|
||||
constructor() {
|
||||
|
||||
this->lastEvent = new event();
|
||||
|
||||
this->lastEvent->mouse->button = -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
event * fetchEvent() {
|
||||
|
||||
event * currentEvent = new event();
|
||||
|
||||
|
||||
currentEvent->mouse->button = -1;
|
||||
|
||||
Window qRoot;
|
||||
|
||||
Window qChild;
|
||||
|
||||
unsigned int qMask;
|
||||
|
||||
int childX;
|
||||
|
||||
int childY;
|
||||
|
||||
int mouseX;
|
||||
|
||||
int mouseY;
|
||||
|
||||
int child;
|
||||
|
||||
|
||||
XWindowAttributes window;
|
||||
|
||||
if( XGetWindowAttributes( this->mainDisplay, this->mainWindow, &window ) ) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if( XQueryPointer( this->mainDisplay, this->RootWindow, &qRoot, &qChild, &mouseX, &mouseY, &childX, &childY, &qMask ) )
|
||||
{
|
||||
|
||||
mouseX -= window.x;
|
||||
|
||||
mouseY -= window.y;
|
||||
|
||||
for(int i = 0; i < sizeof(int) * 8; i++)
|
||||
{
|
||||
int mask = 1 << sizeof(int) * 8 - i - 1;
|
||||
|
||||
if(mask & qMask)
|
||||
{
|
||||
//printf("1");
|
||||
}
|
||||
else
|
||||
{
|
||||
//printf("0");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//printf("(%d)\n", qMask);
|
||||
|
||||
if( qMask == Button1MotionMask ) {
|
||||
|
||||
//printf("LeftMouse\n");
|
||||
|
||||
currentEvent->mouse->button = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( qMask == Button2MotionMask ) {
|
||||
|
||||
printf("Button2MotionMask\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( qMask == Button3MotionMask ) {
|
||||
|
||||
printf("RightMouse\n");
|
||||
|
||||
}
|
||||
|
||||
if( qMask == Button4MotionMask ) {
|
||||
|
||||
printf("Button2MotionMask\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( qMask == Button5MotionMask ) {
|
||||
|
||||
printf("Button2MotionMask\n");
|
||||
|
||||
}
|
||||
|
||||
if( qMask == ShiftMask ) {
|
||||
|
||||
printf("Pressed shift\n");
|
||||
|
||||
}
|
||||
|
||||
if( qMask == ControlMask ) {
|
||||
|
||||
printf("Pressed control\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( qMask == EnterWindowMask ) {
|
||||
|
||||
//printf("EnterWindowMask\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
XEvent event;
|
||||
|
||||
int keyboardEventCount = XPending( this->mainDisplay );
|
||||
|
||||
//printf("eventCount: %i\n\n", keyboardEventCount);
|
||||
|
||||
while( XPending( this->mainDisplay ) ) {
|
||||
|
||||
|
||||
XNextEvent( this->mainDisplay, &event );
|
||||
|
||||
switch ( event.type ) {
|
||||
|
||||
case KeyPress:
|
||||
|
||||
printf("key has been pressed. %i\n\n", event.xkey.keycode);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case KeyRelease:
|
||||
|
||||
printf("key has been released. %i\n\n", event.xkey.keycode);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
case Expose:
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
default:
|
||||
|
||||
//printf("event not defined: %i\n\n", event.type);
|
||||
|
||||
//break;
|
||||
|
||||
}
|
||||
|
||||
//printf("event: %i\n\n", event.type);
|
||||
|
||||
}
|
||||
|
||||
//printf(" Mouse is at (%d,%d)\n", windowX, windowY );
|
||||
|
||||
//printf(" Mouse X: %d mouse Y: %d \n", mouseX, mouseY );
|
||||
|
||||
//XGrabKeyboard( this->mainDisplay, this->RootWindow, 0, GrabModeAsync, GrabModeAsync, CurrentTime);
|
||||
|
||||
|
||||
//printf("qchild is at (%d,%d)\n", mouseX, mouseY);
|
||||
|
||||
}
|
||||
|
||||
bool hasBorder = true;
|
||||
|
||||
int borderCorrection = 0;
|
||||
|
||||
|
||||
|
||||
if( hasBorder ) {
|
||||
|
||||
borderCorrection = 12;
|
||||
|
||||
}
|
||||
|
||||
|
||||
currentEvent->mouse->position = new vector2( mouseX, mouseY + borderCorrection );
|
||||
|
||||
currentEvent->screen->size = new vector2( window.width, window.height + borderCorrection );
|
||||
|
||||
//currentEvent->mouse->eventTypes = new vector();
|
||||
|
||||
vector<char *> * mouseEvents = currentEvent->mouse->eventTypes;
|
||||
|
||||
mouseEvents = new vector();
|
||||
|
||||
if( this->lastEvent->mouse->position.x != currentEvent->mouse->position.x ||
|
||||
this->lastEvent->mouse->position.y != currentEvent->mouse->position.y ) {
|
||||
|
||||
mouseEvents->add( "mousemove" );
|
||||
|
||||
}
|
||||
|
||||
if( this->lastEvent->mouse->button != 0 && currentEvent->mouse->button == 0 ) {
|
||||
|
||||
this->lastMouseDownTime = clock();
|
||||
|
||||
printf("Mouse down\n\n");
|
||||
|
||||
mouseEvents->add( "mousedown" );
|
||||
|
||||
}
|
||||
|
||||
if( this->lastEvent->mouse->button == 0 && currentEvent->mouse->button != 0 ) {
|
||||
|
||||
clock_t difference = clock() - this->lastMouseDownTime;
|
||||
|
||||
int milliseconds = difference * 1000 / CLOCKS_PER_SEC;
|
||||
|
||||
if( milliseconds < 150 ) {
|
||||
|
||||
printf("click event\n\n");
|
||||
|
||||
|
||||
mouseEvents->add( "click" );
|
||||
|
||||
}
|
||||
|
||||
printf("mouseup event\n\n");
|
||||
|
||||
mouseEvents->add( "mouseup" );
|
||||
//printf("%i\n\n", milliseconds);
|
||||
|
||||
|
||||
}
|
||||
|
||||
currentEvent->mouse->eventTypes = mouseEvents;
|
||||
|
||||
|
||||
this->lastEvent = currentEvent;
|
||||
|
||||
return currentEvent;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
167
application/source/engine/floatArray.c
Normal file
167
application/source/engine/floatArray.c
Normal file
@@ -0,0 +1,167 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <char.h>
|
||||
|
||||
#include <text.h>
|
||||
|
||||
#include <vector3.h>
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
class floatArray{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
float * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
float get( int index )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
void set( int index, float item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
float * items = realloc( this->items, sizeof( float ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void addVector2( struct vector2 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
}
|
||||
|
||||
void addVector3( struct vector3 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
this->add( item->z );
|
||||
|
||||
}
|
||||
|
||||
void add( float item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this->items[index] = NULL;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = NULL;
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int array_push( float item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( float item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
float pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
float lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
176
application/source/engine/fontRenderer.c
Normal file
176
application/source/engine/fontRenderer.c
Normal file
@@ -0,0 +1,176 @@
|
||||
|
||||
#include <ft2build.h>
|
||||
|
||||
#include <freetype/freetype.h>
|
||||
|
||||
#include <text.h>
|
||||
|
||||
#include <texture2D.h>
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
|
||||
class fontRenderer{
|
||||
|
||||
GLubyte * data = malloc( 512 * 512 * 4 );
|
||||
|
||||
texture2D * loadFont( char character ) {
|
||||
|
||||
FT_Library ft;
|
||||
|
||||
if ( FT_Init_FreeType( &ft ) )
|
||||
{
|
||||
|
||||
printf("ERROR::FREETYPE: Could not init FreeType Library");
|
||||
|
||||
//return -1;
|
||||
|
||||
} else {
|
||||
|
||||
// printf("FreeType Library loaded\n\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
FT_Face face;
|
||||
|
||||
if ( FT_New_Face( ft, "assets/fonts/WorkSans/WorkSans-Regular.ttf", 0, &face ) )
|
||||
{
|
||||
|
||||
printf("ERROR::FREETYPE: Failed to load font\n\n");
|
||||
|
||||
//return -1;
|
||||
|
||||
} else {
|
||||
|
||||
// printf("FREETYPE: font loaded\n\n");
|
||||
|
||||
}
|
||||
|
||||
int fontSize = 118;
|
||||
|
||||
if ( FT_Set_Pixel_Sizes( face, 0, fontSize ) ) {
|
||||
|
||||
printf("ERROR::FREETYPE: Failed to set font size\n\n");
|
||||
|
||||
} else {
|
||||
|
||||
//printf("FREETYPE: font size set\n\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
FT_Matrix matrix;
|
||||
|
||||
FT_UInt glyph_index;
|
||||
|
||||
FT_Vector pen;
|
||||
|
||||
int n;
|
||||
|
||||
FT_GlyphSlot slot = face->glyph; /* a small shortcut */
|
||||
|
||||
|
||||
pen.x = 0;
|
||||
|
||||
pen.y = 0;
|
||||
|
||||
if( FT_Load_Char( face, character, FT_LOAD_RENDER ) ) {
|
||||
|
||||
printf("ERROR error loading char.");
|
||||
|
||||
} else {
|
||||
|
||||
//printf("FREETYPE: Char loaded %c \n", character );
|
||||
|
||||
}
|
||||
|
||||
|
||||
//printf( "%c: bitmap_left: %i, bitmap_top: %i\n", ( char ) character, slot->bitmap_left, slot->bitmap_top );
|
||||
|
||||
|
||||
FT_Bitmap * bitmap = &slot->bitmap;
|
||||
|
||||
texture2D * texture = new texture2D();
|
||||
|
||||
texture->width = bitmap->width;
|
||||
|
||||
texture->height = bitmap->rows;
|
||||
|
||||
texture->hasAlpha = 1;
|
||||
|
||||
texture->offset = new vector2( ( float ) slot->bitmap_left, ( float ) slot->bitmap_top );
|
||||
|
||||
|
||||
//printf("x_max: %i\n", texture->width);
|
||||
|
||||
//printf("y_max: %i\n", texture->height);
|
||||
|
||||
|
||||
|
||||
|
||||
texture->data = bitmap->buffer;//bitmap->buffer;
|
||||
|
||||
for (int x = 0; x < texture->width; ++x)
|
||||
{
|
||||
//printf("%u ", (unsigned int)bitmap->buffer[0]);
|
||||
}
|
||||
|
||||
//FT_Done_Face(face);
|
||||
|
||||
//FT_Done_FreeType(ft);
|
||||
|
||||
|
||||
|
||||
return texture;
|
||||
|
||||
//this->show_image();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void draw_bitmap( FT_Bitmap * bitmap, FT_Int x, FT_Int y ) {
|
||||
|
||||
FT_Int i, j, p, q;
|
||||
|
||||
FT_Int x_max = x + bitmap->width;
|
||||
FT_Int y_max = y + bitmap->rows;
|
||||
|
||||
int WIDTH = 512;
|
||||
|
||||
int HEIGHT = 512;
|
||||
|
||||
printf("x_max: %i\n", x_max);
|
||||
|
||||
printf("y_max: %i\n", y_max);
|
||||
|
||||
|
||||
for ( i = x, p = 0; i < x_max; i++, p++ )
|
||||
{
|
||||
for ( j = y, q = 0; j < y_max; j++, q++ )
|
||||
{
|
||||
//if ( i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT )
|
||||
//continue;
|
||||
|
||||
//this->data[j + (i * 512)] |= bitmap->buffer[q * bitmap->width + p];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void show_image( void )
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
11
application/source/engine/hints.c
Normal file
11
application/source/engine/hints.c
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
|
||||
class Hints
|
||||
{
|
||||
unsigned long flags;
|
||||
unsigned long functions;
|
||||
unsigned long decorations;
|
||||
long inputMode;
|
||||
unsigned long status;
|
||||
}
|
||||
28
application/source/engine/member.c
Normal file
28
application/source/engine/member.c
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
|
||||
|
||||
class member{
|
||||
|
||||
char * name;
|
||||
|
||||
GLint index;
|
||||
|
||||
GLint offset;
|
||||
|
||||
GLint size;
|
||||
|
||||
GLenum type;
|
||||
|
||||
GLuint arrayStride;
|
||||
|
||||
GLuint topLevelSize;
|
||||
|
||||
}
|
||||
703
application/source/engine/mesh.c
Normal file
703
application/source/engine/mesh.c
Normal file
@@ -0,0 +1,703 @@
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include "block.h"
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
#include "program.h"
|
||||
|
||||
#include "floatArray.h"
|
||||
|
||||
#include "unsignedIntegerArray.h"
|
||||
|
||||
|
||||
|
||||
class mesh{
|
||||
|
||||
struct program * program;
|
||||
|
||||
struct unsignedIntegerArray * indices;
|
||||
|
||||
|
||||
struct floatArray * textureCoordinates;
|
||||
|
||||
struct floatArray * vertexCoordinates;
|
||||
|
||||
struct floatArray * normalCoordinates;
|
||||
|
||||
|
||||
struct array * blocks = new array();
|
||||
|
||||
|
||||
GLuint vertexArrayObject;
|
||||
|
||||
|
||||
GLuint uniformBuffer;
|
||||
|
||||
GLuint indexBuffer;
|
||||
|
||||
GLuint vertexbuffer;
|
||||
|
||||
GLuint textureCoordinateBuffer;
|
||||
|
||||
GLuint meshIndexBuffer;
|
||||
|
||||
GLuint uvBuffer;
|
||||
|
||||
struct unsignedIntegerArray * uniformBuffers = new unsignedIntegerArray();
|
||||
|
||||
|
||||
|
||||
struct block * getUniformBlock( char * blockName ) {
|
||||
|
||||
int blockCount = this->blocks->length();
|
||||
|
||||
for ( int i = 0; i < blockCount; ++i )
|
||||
{
|
||||
struct block * currentBlock = this->blocks->get( i );
|
||||
|
||||
char * currentBlockName = currentBlock->name;
|
||||
|
||||
if( currentBlockName == blockName ) {
|
||||
|
||||
return currentBlock;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void bindBlock( struct block * blockInstance ) {
|
||||
|
||||
//printf("%s\n", blockInstance);
|
||||
|
||||
blockInstance->createBuffer();
|
||||
|
||||
this->blocks->add( blockInstance );
|
||||
|
||||
}
|
||||
|
||||
void setProgram( struct program * currentProgram ) {
|
||||
|
||||
this->program = currentProgram;
|
||||
|
||||
}
|
||||
|
||||
GLuint getGLTypeSize( GLuint type ) {
|
||||
|
||||
switch( type ) {
|
||||
|
||||
case GL_FLOAT:
|
||||
|
||||
return sizeof( GLfloat );
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT:
|
||||
|
||||
return sizeof( GLint );
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_UNSIGNED_INT:
|
||||
|
||||
return sizeof( GLuint );
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
GLuint getComponentType( GLuint type ) {
|
||||
|
||||
|
||||
switch( type ) {
|
||||
|
||||
case GL_FLOAT:
|
||||
|
||||
return GL_FLOAT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
return GL_FLOAT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
return GL_FLOAT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC4:
|
||||
|
||||
return GL_FLOAT;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_INT:
|
||||
|
||||
return GL_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC2:
|
||||
|
||||
return GL_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC3:
|
||||
|
||||
return GL_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC4:
|
||||
|
||||
return GL_INT;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_UNSIGNED_INT:
|
||||
|
||||
return GL_UNSIGNED_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC2:
|
||||
|
||||
return GL_UNSIGNED_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC3:
|
||||
|
||||
return GL_UNSIGNED_INT;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC4:
|
||||
|
||||
return GL_UNSIGNED_INT;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
GLuint getItemSize( GLuint type ) {
|
||||
|
||||
switch( type ) {
|
||||
|
||||
case GL_FLOAT:
|
||||
|
||||
return 1;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
return 2;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
return 3;
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC4:
|
||||
|
||||
return 4;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_INT:
|
||||
|
||||
return 1;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC2:
|
||||
|
||||
return 2;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC3:
|
||||
|
||||
return 3;
|
||||
|
||||
break;
|
||||
|
||||
case GL_INT_VEC4:
|
||||
|
||||
return 4;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_UNSIGNED_INT:
|
||||
|
||||
return 1;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC2:
|
||||
|
||||
return 2;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC3:
|
||||
|
||||
return 3;
|
||||
|
||||
break;
|
||||
|
||||
case GL_UNSIGNED_INT_VEC4:
|
||||
|
||||
return 4;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
GLuint createBuffer( char * attributeName, void * data, GLenum target, GLenum usage ) {
|
||||
|
||||
GLuint itemSize;
|
||||
|
||||
GLuint componentType;
|
||||
|
||||
GLuint componentSize;
|
||||
|
||||
GLuint attributeLocation;
|
||||
|
||||
//GLenum target;
|
||||
|
||||
GLuint isIndexBuffer = 0;
|
||||
|
||||
GLuint buffer;
|
||||
|
||||
glGenBuffers( 1, &buffer );
|
||||
|
||||
|
||||
if( attributeName == "index" ) {
|
||||
|
||||
isIndexBuffer = 1;
|
||||
|
||||
componentType = GL_INT;
|
||||
|
||||
componentSize = this->getGLTypeSize( componentType );
|
||||
|
||||
target = GL_ELEMENT_ARRAY_BUFFER;
|
||||
|
||||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffer );
|
||||
|
||||
} else {
|
||||
|
||||
attribute * attribute = this->program->getAttributeByName( attributeName );
|
||||
|
||||
if( attribute == NULL ) {
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
itemSize = this->getItemSize( attribute->type );
|
||||
|
||||
componentType = this->getComponentType( attribute->type );
|
||||
|
||||
componentSize = this->getGLTypeSize( componentType );
|
||||
|
||||
attributeLocation = attribute->location;
|
||||
|
||||
target = GL_ARRAY_BUFFER;
|
||||
|
||||
glEnableVertexAttribArray( attribute->location );
|
||||
|
||||
glBindBuffer( GL_ARRAY_BUFFER, buffer );
|
||||
|
||||
}
|
||||
|
||||
//printf("Name: %s itemSize: %i componentSize: %i ", attributeName, itemSize, componentSize);
|
||||
|
||||
|
||||
int itemCount;
|
||||
|
||||
GLuint bufferSize;
|
||||
|
||||
|
||||
if( componentType == GL_UNSIGNED_INT || componentType == GL_INT ) {
|
||||
|
||||
unsignedIntegerArray * test = ( unsignedIntegerArray * ) data;
|
||||
|
||||
itemCount = test->length();
|
||||
|
||||
bufferSize = itemCount * componentSize;
|
||||
|
||||
glBufferData( target, bufferSize, test->items, usage );
|
||||
|
||||
if( isIndexBuffer == NULL ) {
|
||||
|
||||
glVertexAttribIPointer( attributeLocation, itemSize, componentType, 0, ( void * ) 0 );
|
||||
|
||||
}
|
||||
|
||||
} else if( componentType == GL_FLOAT ) {
|
||||
|
||||
floatArray * test = ( floatArray * ) data;
|
||||
|
||||
itemCount = test->total;
|
||||
|
||||
bufferSize = itemCount * componentSize;
|
||||
|
||||
glBufferData( GL_ARRAY_BUFFER, bufferSize, test->items, usage );
|
||||
|
||||
if( isIndexBuffer == NULL ) {
|
||||
|
||||
glVertexAttribPointer( attributeLocation, itemSize, componentType, GL_FALSE, 0, ( void * ) 0 );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//printf("itemCount: %i\n\n", bufferSize);
|
||||
|
||||
GLint compareSize = 0;
|
||||
|
||||
glGetBufferParameteriv( target, GL_BUFFER_SIZE, &compareSize );
|
||||
|
||||
if( bufferSize != compareSize )
|
||||
{
|
||||
|
||||
glDeleteBuffers(1, &buffer);
|
||||
|
||||
printf("ERROR: size error");
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
return buffer;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void createBuffers( ) {
|
||||
|
||||
struct unsignedIntegerArray * indices = new unsignedIntegerArray();
|
||||
|
||||
struct floatArray * textureCoordinates = new floatArray();
|
||||
|
||||
struct floatArray * vertexCoordinates = new floatArray();
|
||||
|
||||
struct floatArray * normalCoordinates = new floatArray();
|
||||
|
||||
struct unsignedIntegerArray * meshIndices = new unsignedIntegerArray();
|
||||
|
||||
int subdivisionsDepth = 1;
|
||||
|
||||
int subdivisionsWidth = 1;
|
||||
|
||||
float width = 2;
|
||||
|
||||
float depth = 2;
|
||||
|
||||
|
||||
int meshCount = 100 * 50;
|
||||
|
||||
int meshStartIndex = 0;
|
||||
|
||||
for (int meshIndex = 0; meshIndex < meshCount; ++meshIndex)
|
||||
{
|
||||
|
||||
|
||||
meshStartIndex = meshIndices->length();
|
||||
|
||||
//printf("Number of vectex cnumVertsAcrossoordinates: %i\n\n", vertexCoordinates->length());
|
||||
|
||||
int numVertsAcross = subdivisionsWidth + 1;
|
||||
|
||||
for ( int z = 0; z < subdivisionsDepth; z++ ) {
|
||||
|
||||
for ( int x = 0; x < subdivisionsWidth; x++ ) {
|
||||
|
||||
// triangle 1 of quad
|
||||
indices->add( ( z + 0 ) * numVertsAcross + x + meshStartIndex );
|
||||
|
||||
indices->add( ( z + 1 ) * numVertsAcross + x + meshStartIndex );
|
||||
|
||||
indices->add( ( z + 0 ) * numVertsAcross + x + 1 + meshStartIndex );
|
||||
|
||||
// triangle 2 of quad
|
||||
indices->add( ( z + 1 ) * numVertsAcross + x + meshStartIndex );
|
||||
|
||||
indices->add( ( z + 1 ) * numVertsAcross + x + 1 + meshStartIndex );
|
||||
|
||||
indices->add( ( z + 0 ) * numVertsAcross + x + 1 + meshStartIndex );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
for ( int z = 0; z <= subdivisionsDepth; z++ ) {
|
||||
|
||||
for ( int x = 0; x <= subdivisionsWidth; x++ ) {
|
||||
|
||||
|
||||
float u = ( float ) x;// / subdivisionsWidth;
|
||||
|
||||
float v = ( float ) z;// / subdivisionsDepth;
|
||||
|
||||
|
||||
textureCoordinates->add( u );
|
||||
|
||||
textureCoordinates->add( ( 1 - v ) );
|
||||
|
||||
|
||||
vertexCoordinates->add( width * u - width * 0.5 );
|
||||
|
||||
vertexCoordinates->add( depth * v - depth * 0.5 );
|
||||
|
||||
vertexCoordinates->add( 0 );
|
||||
|
||||
|
||||
normalCoordinates->add( 0 );
|
||||
|
||||
normalCoordinates->add( 0 );
|
||||
|
||||
normalCoordinates->add( 1 );
|
||||
|
||||
|
||||
meshIndices->add( meshIndex );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
glGenVertexArrays( 1, &this->vertexArrayObject );
|
||||
|
||||
glBindVertexArray( this->vertexArrayObject );
|
||||
|
||||
|
||||
this->vertexbuffer = this->createBuffer( "position", vertexCoordinates, GL_ARRAY_BUFFER, GL_STATIC_DRAW );
|
||||
|
||||
this->textureCoordinateBuffer = this->createBuffer( "textureCoordinates", textureCoordinates, GL_ARRAY_BUFFER, GL_STATIC_DRAW );
|
||||
|
||||
this->vertexbuffer = this->createBuffer( "meshIndex", meshIndices, GL_ARRAY_BUFFER, GL_STATIC_DRAW );
|
||||
|
||||
this->indexBuffer = this->createBuffer( "index", indices, GL_ARRAY_BUFFER, GL_STATIC_DRAW );
|
||||
|
||||
//this->subMeshPositions = this->createBuffer( "subMeshPositions", indices, GL_SHADER_STORAGE_BUFFER, GL_STATIC_DRAW );
|
||||
|
||||
int blockCount = this->blocks->length();
|
||||
|
||||
for (int i = 0; i < blockCount; ++i)
|
||||
{
|
||||
|
||||
block * currentBlock = this->blocks->get( i );
|
||||
|
||||
int size = currentBlock->bufferSize;
|
||||
|
||||
printf("Bind block %s bufferSize: %i bindingPoint: %i bufferType: %s \n\n", currentBlock->name, currentBlock->bufferSize, currentBlock->bindingPoint, currentBlock->getBufferTypeText( ) );
|
||||
|
||||
|
||||
|
||||
glBindBuffer( currentBlock->bufferType, currentBlock->buffer );
|
||||
|
||||
glBindBufferBase( currentBlock->bufferType, currentBlock->bindingPoint, currentBlock->buffer );
|
||||
|
||||
glBufferData( currentBlock->bufferType, currentBlock->bufferSize, 0, GL_DYNAMIC_DRAW );
|
||||
|
||||
}
|
||||
|
||||
|
||||
glBindVertexArray( 0 );
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
unsigned int uniformBufferSize = 152; // allocate 152 bytes of memory
|
||||
|
||||
glGenBuffers( 1, &this->uniformBuffer );
|
||||
|
||||
glBindBuffer( GL_UNIFORM_BUFFER, this->uniformBuffer );
|
||||
|
||||
glBufferData( GL_UNIFORM_BUFFER, uniformBufferSize, NULL, GL_DYNAMIC_DRAW );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
glBindBuffer( GL_UNIFORM_BUFFER, this->uniformBuffer );
|
||||
|
||||
//glBufferData( this->uniformBuffer, size, data );
|
||||
|
||||
glBufferSubData( this->uniformBuffer, offset, size, data );
|
||||
*/
|
||||
|
||||
//glBindBufferRange( GL_UNIFORM_BUFFER, 0, uboMatrices, 0, 2 * sizeof(glm::mat4) );
|
||||
|
||||
|
||||
/*
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width/(float)height, 0.1f, 100.0f);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(projection));
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
|
||||
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(view));
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0); */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, indexBufferSize, indices->items );
|
||||
|
||||
|
||||
/*
|
||||
int vertexCount = vertexCoordinates->length();
|
||||
|
||||
for (int i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
|
||||
float currentVertex = vertexCoordinates->get( i );
|
||||
|
||||
printf(" %i: %f \n", i, currentVertex);
|
||||
|
||||
}
|
||||
|
||||
int indexCount = indices->length();
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
for (int i = 0; i < indexCount; ++i)
|
||||
{
|
||||
|
||||
int currentIndex = indices->get( i );
|
||||
|
||||
printf(" %i: %i \n", i, currentIndex);
|
||||
|
||||
}
|
||||
|
||||
printf("number of indices: %i\n", indices->length());
|
||||
*/
|
||||
|
||||
this->indices = indices;
|
||||
|
||||
this->textureCoordinates = textureCoordinates;
|
||||
|
||||
this->vertexCoordinates = vertexCoordinates;
|
||||
|
||||
this->normalCoordinates = normalCoordinates;
|
||||
|
||||
}
|
||||
|
||||
createOrderedTriangleStripQuad() {
|
||||
|
||||
|
||||
/*
|
||||
vector3 * a = new vector3(0.0, 0.0, 0.0);
|
||||
|
||||
vector3 * b = new vector3(0.0, 0.5, 0.0);
|
||||
|
||||
vector3 * c = new vector3(0.5, 0.0, 0.0);
|
||||
|
||||
vector3 * d = new vector3(0.5, 0.5, 0.0);
|
||||
|
||||
GLfloat g_vertex_buffer_data[ 12 * 2 ];
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
|
||||
int vectorIndex = i * 3;
|
||||
|
||||
float index = 0.5 *i;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 0 ] = 0.0 - index;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 1 ] = 0.;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 2 ] = 0.0;
|
||||
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 3 ] = 0.0 - index;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 4 ] = 0.5;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 5 ] = 0.0;
|
||||
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 6 ] = 0.5 - index;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 7 ] = 0.0;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 8 ] = 0.0;
|
||||
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 9 ] = 0.5 - index;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 10 ] = 0.5;
|
||||
|
||||
g_vertex_buffer_data[ vectorIndex + 11 ] = 0.0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
GLfloat g_vertex_buffer_data[] = {
|
||||
0.0, 0.0, 0.0f,
|
||||
0.0, 0.5, 0.0f,
|
||||
0.5f, 0.0f, 0.0f,
|
||||
.5, 0.5, 0.0
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
338
application/source/engine/opengl.c
Normal file
338
application/source/engine/opengl.c
Normal file
@@ -0,0 +1,338 @@
|
||||
|
||||
#include "./mesh.h"
|
||||
|
||||
#include "./shader.h"
|
||||
|
||||
#include "./sampler2D.h"
|
||||
|
||||
#include "./texture2D.h"
|
||||
|
||||
#include "./floatArray.h"
|
||||
|
||||
#include "./unsignedIntegerArray.h"
|
||||
|
||||
#include "./eventManager.h"
|
||||
|
||||
#include "./pipeline.h"
|
||||
|
||||
|
||||
#include "./renderPasses/renderPassQuads.h"
|
||||
|
||||
#include "./renderPasses/renderPassFont.h"
|
||||
|
||||
#include "./renderPasses/renderPassCompute.h"
|
||||
|
||||
#include "./renderPasses/renderPassCompute2.h"
|
||||
|
||||
#include "./renderPasses/renderPassTesselation.h"
|
||||
|
||||
|
||||
|
||||
#include "./resourceManager.h"
|
||||
|
||||
#include "./windowManager.h"
|
||||
|
||||
#include "./event.h"
|
||||
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
|
||||
|
||||
|
||||
event * globalEvent;
|
||||
|
||||
resourceManager * resources;
|
||||
|
||||
|
||||
|
||||
class opengl{
|
||||
|
||||
Display * mainDisplay;
|
||||
|
||||
Window mainWindow;
|
||||
|
||||
int MainScreen;
|
||||
|
||||
Window RootWindow;
|
||||
|
||||
|
||||
int lastTime = clock();
|
||||
|
||||
struct timespec startTime;
|
||||
|
||||
int frameCount = 0;
|
||||
|
||||
|
||||
|
||||
sampler2D * testSampler;
|
||||
|
||||
struct windowManager * windowManager = new windowManager();
|
||||
|
||||
|
||||
|
||||
struct eventManger * eventManger = new eventManger();
|
||||
|
||||
struct pipeline * pipeline = new pipeline();
|
||||
|
||||
|
||||
|
||||
|
||||
initialize() {
|
||||
|
||||
printf("initialize opengl.\n");
|
||||
|
||||
resources = new resourceManager();
|
||||
|
||||
this->setupWindow();
|
||||
|
||||
this->setupManagers();
|
||||
|
||||
this->showVersion();
|
||||
|
||||
//this->createTexture();
|
||||
|
||||
this->setupPipeline();
|
||||
|
||||
//this->loadFont();
|
||||
|
||||
this->setupTime();
|
||||
|
||||
this->setupRenderLoop();
|
||||
|
||||
}
|
||||
|
||||
showExtensions() {
|
||||
|
||||
GLint max_layers;
|
||||
|
||||
glGetIntegerv ( GL_MAX_ARRAY_TEXTURE_LAYERS, &max_layers );
|
||||
|
||||
printf("GL_MAX_ARRAY_TEXTURE_LAYERS: %i\n", max_layers);
|
||||
|
||||
|
||||
GLint max_texture_size;
|
||||
|
||||
glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max_texture_size);
|
||||
|
||||
printf("GL_MAX_TEXTURE_SIZE: %i\n", max_texture_size);
|
||||
|
||||
|
||||
}
|
||||
|
||||
showVersion() {
|
||||
|
||||
printf("opengl version : %s\n\n", glGetString(GL_VERSION) );
|
||||
|
||||
}
|
||||
|
||||
setupTime() {
|
||||
|
||||
clock_gettime( CLOCK_REALTIME, &this->startTime );
|
||||
|
||||
}
|
||||
|
||||
setupManagers() {
|
||||
|
||||
this->eventManger->mainDisplay = this->mainDisplay;
|
||||
|
||||
this->eventManger->mainWindow = this->mainWindow;
|
||||
|
||||
this->eventManger->RootWindow = this->RootWindow;
|
||||
|
||||
}
|
||||
|
||||
setupWindow() {
|
||||
|
||||
this->windowManager->setupDisplay();
|
||||
|
||||
this->windowManager->setupWindow();
|
||||
|
||||
|
||||
this->mainDisplay = this->windowManager->mainDisplay;
|
||||
|
||||
this->mainWindow = this->windowManager->mainWindow;
|
||||
|
||||
this->RootWindow = this->windowManager->RootWindow;
|
||||
|
||||
}
|
||||
|
||||
setupRenderLoop() {
|
||||
|
||||
int IsProgramRunning = 1;
|
||||
|
||||
while( IsProgramRunning ) {
|
||||
|
||||
while( XPending( this->mainDisplay ) ) {
|
||||
|
||||
XEvent GeneralEvent = {};
|
||||
|
||||
XNextEvent( this->mainDisplay, &GeneralEvent );
|
||||
|
||||
switch( GeneralEvent.type ) {
|
||||
|
||||
case ClientMessage:
|
||||
|
||||
|
||||
IsProgramRunning = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this->render();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setupPipeline() {
|
||||
|
||||
quads * quadsPass = new quads();
|
||||
|
||||
font * fontPass = new font();
|
||||
|
||||
compute * computePass = new compute();
|
||||
|
||||
compute2 * computePass2 = new compute2();
|
||||
|
||||
tesselation * tesselationPass = new tesselation();
|
||||
|
||||
|
||||
//this->pipeline->addRenderPass( fontPass );
|
||||
|
||||
this->pipeline->addRenderPass( quadsPass );
|
||||
|
||||
//this->pipeline->addRenderPass( computePass );
|
||||
|
||||
//this->pipeline->addRenderPass( computePass2 );
|
||||
|
||||
//this->pipeline->addRenderPass( tesselationPass );
|
||||
|
||||
|
||||
}
|
||||
|
||||
double clockToMilliseconds( clock_t ticks ){
|
||||
|
||||
return ( ticks / ( double ) CLOCKS_PER_SEC );
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
//this->clearColor( 0.0, 0.0, 0.0, 0.0 );
|
||||
|
||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
this->clear( GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
globalEvent = this->eventManger->fetchEvent();
|
||||
|
||||
this->pipeline->render();
|
||||
|
||||
this->swapBuffers();
|
||||
|
||||
//this->displayFPS();
|
||||
|
||||
}
|
||||
|
||||
displayFPS() {
|
||||
|
||||
struct timespec now;
|
||||
|
||||
clock_gettime( CLOCK_REALTIME, &now );
|
||||
|
||||
this->frameCount++;
|
||||
|
||||
int elapsedTime = now.tv_sec - this->startTime.tv_sec;
|
||||
|
||||
if( elapsedTime != this->lastTime ) {
|
||||
|
||||
printf("%i fps.\n\n", this->frameCount );
|
||||
|
||||
this->lastTime = elapsedTime;
|
||||
|
||||
this->frameCount = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
clear( GLbitfield mask ) {
|
||||
|
||||
glClear( mask );
|
||||
|
||||
}
|
||||
|
||||
clearColor( float r, float g, float b, float a ) {
|
||||
|
||||
glClearColor( r, g, b, a );
|
||||
|
||||
}
|
||||
|
||||
swapBuffers() {
|
||||
|
||||
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
|
||||
|
||||
PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
|
||||
|
||||
PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
|
||||
|
||||
|
||||
glXSwapIntervalEXT = ( PFNGLXSWAPINTERVALEXTPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalEXT" );
|
||||
|
||||
if ( glXSwapIntervalEXT != NULL ) {
|
||||
|
||||
glXSwapIntervalEXT( this->mainDisplay, this->mainWindow, 0 );
|
||||
|
||||
} else {
|
||||
|
||||
glXSwapIntervalMESA = ( PFNGLXSWAPINTERVALMESAPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalMESA" );
|
||||
|
||||
if ( glXSwapIntervalMESA != NULL ) {
|
||||
|
||||
glXSwapIntervalMESA( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
glXSwapIntervalSGI = ( PFNGLXSWAPINTERVALSGIPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalSGI" );
|
||||
|
||||
if ( glXSwapIntervalSGI != NULL ) {
|
||||
|
||||
glXSwapIntervalSGI( 0 );
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glXSwapBuffers( this->mainDisplay, this->mainWindow );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
82
application/source/engine/pipeline.c
Normal file
82
application/source/engine/pipeline.c
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
#include "../int.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "./renderPasses/renderPass.h"
|
||||
|
||||
#include "./vector.h"
|
||||
|
||||
#include "../classConfiguration.h"
|
||||
|
||||
|
||||
|
||||
reflect
|
||||
class pipeline{
|
||||
|
||||
array * renderPasses = new array();
|
||||
|
||||
vector<int> * classIndices = new vector();
|
||||
|
||||
vector<int> * methodIndices = new vector();
|
||||
|
||||
addRenderPass( ... ) {
|
||||
|
||||
va_list args;
|
||||
|
||||
va_start( args, count );
|
||||
|
||||
int classIndex = datatypes[0];
|
||||
|
||||
this->classIndices->add( classIndex );
|
||||
|
||||
|
||||
|
||||
void * voidPointer = va_arg( args, void * );
|
||||
|
||||
int methodIndex = getMethodIndexByPropertyName( classIndex, "prepare" );
|
||||
|
||||
int renderMethodIndex = getMethodIndexByPropertyName( classIndex, "render" );
|
||||
|
||||
|
||||
this->methodIndices->add( renderMethodIndex );
|
||||
|
||||
|
||||
|
||||
int classIndexTest = this->classIndices->get( 0 );
|
||||
|
||||
//printf("\n\n\n\n%i\n\n\n\n", classIndexTest);
|
||||
|
||||
callMethodOfClass( classIndex, methodIndex, voidPointer );
|
||||
|
||||
printf("\n");
|
||||
|
||||
va_end( args );
|
||||
|
||||
|
||||
this->renderPasses->add( voidPointer );
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
array * renderPasses = this->renderPasses;
|
||||
|
||||
int renderPassCount = renderPasses->length();
|
||||
|
||||
for (int i = 0; i < renderPassCount; ++i)
|
||||
{
|
||||
|
||||
int classIndex = this->classIndices->get( i );
|
||||
|
||||
void * voidPointer = renderPasses->get( i );
|
||||
|
||||
int methodIndex = this->methodIndices->get( i );
|
||||
|
||||
callMethodOfClass( classIndex, methodIndex, voidPointer );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
534
application/source/engine/program.c
Normal file
534
application/source/engine/program.c
Normal file
@@ -0,0 +1,534 @@
|
||||
|
||||
#include "block.h"
|
||||
|
||||
#include "member.h"
|
||||
|
||||
#include "uniform.h"
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fileSystem.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "../char.h"
|
||||
|
||||
#include "sampler2D.h"
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
class program{
|
||||
|
||||
|
||||
GLuint samplerIndex = 0;
|
||||
|
||||
array * uniforms = new array();
|
||||
|
||||
array * attributes = new array();
|
||||
|
||||
array * blocks = new array();
|
||||
|
||||
array * shaders = new array();
|
||||
|
||||
GLuint glProgram;
|
||||
|
||||
|
||||
|
||||
addShader( struct shader * shaderInstance ) {
|
||||
|
||||
this->shaders->add( shaderInstance );
|
||||
|
||||
}
|
||||
|
||||
|
||||
GLint glGetProgramResourceiv( GLint programProperty, GLint index, GLint Property ) {
|
||||
|
||||
GLint offsetValues;
|
||||
|
||||
glGetProgramResourceiv( this->glProgram, programProperty, index, 1, &Property , 1, 0, &offsetValues );
|
||||
|
||||
return offsetValues;
|
||||
|
||||
}
|
||||
|
||||
GLchar * glGetProgramResourceName( GLint programProperty, GLint index, GLint nameLength ) {
|
||||
|
||||
|
||||
GLchar name[ GL_NAME_LENGTH + 1 ];
|
||||
|
||||
glGetProgramResourceName(
|
||||
this->glProgram,
|
||||
|
||||
programProperty,
|
||||
index,
|
||||
GL_NAME_LENGTH + 1,
|
||||
0,
|
||||
name );
|
||||
|
||||
|
||||
return name;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void extractBlocks() {
|
||||
|
||||
GLint programInterfaces[2] = { GL_SHADER_STORAGE_BLOCK, GL_UNIFORM_BLOCK }; // GL_UNIFORM_BLOCK
|
||||
|
||||
GLint programProperties[2] = { GL_BUFFER_VARIABLE, GL_UNIFORM }; // GL_UNIFORM , GL_PROGRAM_INPUT
|
||||
|
||||
GLenum programBufferTypes[2] = { GL_SHADER_STORAGE_BUFFER, GL_UNIFORM_BUFFER };
|
||||
|
||||
|
||||
|
||||
for (int blockTypeIndex = 0; blockTypeIndex < 2; ++blockTypeIndex)
|
||||
{
|
||||
|
||||
|
||||
GLint program = this->glProgram;
|
||||
|
||||
GLint numActiveResources;
|
||||
|
||||
GLint programInterface = programInterfaces[blockTypeIndex]; // GL_UNIFORM_BLOCK
|
||||
|
||||
GLint programProperty = programProperties[blockTypeIndex]; // GL_UNIFORM
|
||||
|
||||
GLenum programBufferType = programBufferTypes[blockTypeIndex];
|
||||
|
||||
glGetProgramInterfaceiv( program, programInterface, GL_ACTIVE_RESOURCES, &numActiveResources );
|
||||
|
||||
//printf("extractShaderStorageBuffers %i\n\n", numActiveResources);
|
||||
|
||||
for ( GLuint blockIndex = 0; blockIndex < numActiveResources; blockIndex++ ) {
|
||||
|
||||
struct block * blockInstance = new block();
|
||||
|
||||
//GLint blockIndex = this->glGetProgramResourceiv( programInterface, blockIndex, GL_BLOCK_INDEX );
|
||||
|
||||
GLint blockNameLength = this->glGetProgramResourceiv( programInterface, blockIndex, GL_NAME_LENGTH );
|
||||
|
||||
blockInstance->bufferSize = this->glGetProgramResourceiv( programInterface, blockIndex, GL_BUFFER_DATA_SIZE );
|
||||
|
||||
GLint numberActiveVariables = this->glGetProgramResourceiv( programInterface, blockIndex, GL_NUM_ACTIVE_VARIABLES );
|
||||
|
||||
blockInstance->bindingPoint = this->glGetProgramResourceiv( programInterface, blockIndex, GL_BUFFER_BINDING );
|
||||
|
||||
//const GLchar * name = this->glGetProgramResourceName( programInterface, blockIndex, blockNameLength );
|
||||
GLchar name[ GL_NAME_LENGTH + 1 ];
|
||||
|
||||
glGetProgramResourceName( this->glProgram, programInterface, blockIndex, GL_NAME_LENGTH + 1, NULL, name );
|
||||
|
||||
blockInstance->name = malloc( GL_NAME_LENGTH );
|
||||
|
||||
strcpy( blockInstance->name, name );
|
||||
|
||||
|
||||
blockInstance->bufferType = programBufferType;
|
||||
|
||||
printf("block:%s \n\n", blockInstance->name );
|
||||
|
||||
printf(" block name %s\n", blockInstance->name);
|
||||
|
||||
printf(" block buffer size: %i\n", blockInstance->bufferSize);
|
||||
|
||||
printf(" block binding point: %i\n\n", blockInstance->bindingPoint);
|
||||
|
||||
|
||||
|
||||
|
||||
GLint indices[ numberActiveVariables ];
|
||||
|
||||
GLenum member = GL_ACTIVE_VARIABLES;
|
||||
|
||||
glGetProgramResourceiv( program, programInterface, blockIndex, 1, &member, numberActiveVariables, 0, indices );
|
||||
|
||||
printf(" number of variables: %i\n\n\n", numberActiveVariables);
|
||||
|
||||
// add block
|
||||
|
||||
|
||||
for ( GLuint index = 0; index < numberActiveVariables; index++ )
|
||||
{
|
||||
|
||||
struct member * memberInstance = new member();
|
||||
|
||||
|
||||
GLint itemIndex = indices[ index ];
|
||||
|
||||
memberInstance->index = itemIndex;
|
||||
|
||||
GLint nameLength = this->glGetProgramResourceiv( programProperty, itemIndex, GL_NAME_LENGTH );
|
||||
|
||||
memberInstance->offset = this->glGetProgramResourceiv( programProperty, itemIndex, GL_OFFSET );
|
||||
|
||||
memberInstance->type = this->glGetProgramResourceiv( programProperty, itemIndex, GL_TYPE );
|
||||
|
||||
memberInstance->arrayStride = this->glGetProgramResourceiv( programProperty, itemIndex, GL_ARRAY_STRIDE );
|
||||
|
||||
|
||||
memberInstance->size = this->glGetProgramResourceiv( programProperty, itemIndex, GL_ARRAY_SIZE );
|
||||
|
||||
int topLevelSize = this->glGetProgramResourceiv( programProperty, itemIndex, GL_TOP_LEVEL_ARRAY_SIZE );
|
||||
|
||||
int topLevelStride = this->glGetProgramResourceiv( programProperty, itemIndex, GL_TOP_LEVEL_ARRAY_STRIDE );
|
||||
|
||||
|
||||
if( memberInstance->arrayStride == 0 ) {
|
||||
|
||||
memberInstance->arrayStride = topLevelStride;
|
||||
|
||||
}
|
||||
|
||||
memberInstance->topLevelSize = topLevelSize;
|
||||
|
||||
|
||||
// memberInstance->name = this->glGetProgramResourceName( programProperty, itemIndex, nameLength );
|
||||
|
||||
|
||||
GLchar memberName[ GL_NAME_LENGTH + 1 ];
|
||||
|
||||
glGetProgramResourceName( this->glProgram, programProperty, itemIndex, GL_NAME_LENGTH + 1, NULL, memberName );
|
||||
|
||||
memberInstance->name = malloc( GL_NAME_LENGTH );
|
||||
|
||||
strcpy( memberInstance->name, memberName );
|
||||
|
||||
|
||||
|
||||
printf(" offset: #%i name: %s vec2: %i offset: %i itemSize / arrayStride: %i Array size: %i toplevel size: %i top level size: %i \n\n",
|
||||
|
||||
memberInstance->index,
|
||||
memberInstance->name,
|
||||
memberInstance->type == GL_FLOAT_VEC2,
|
||||
memberInstance->offset,
|
||||
memberInstance->arrayStride,
|
||||
memberInstance->size, topLevelSize,
|
||||
topLevelStride );
|
||||
|
||||
|
||||
|
||||
blockInstance->add( memberInstance );
|
||||
|
||||
}
|
||||
|
||||
blockInstance->createBuffer();
|
||||
|
||||
this->blocks->add( blockInstance );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
struct block * createNewBlock( char * blockName ) {
|
||||
|
||||
printf("Copy buffer: %s\n\n", blockName);
|
||||
|
||||
block * originalBlock = this->getBlock( blockName );
|
||||
|
||||
block * blockCopy = new block();
|
||||
|
||||
blockCopy->members = originalBlock->members;
|
||||
|
||||
blockCopy->bindingPoint = originalBlock->bindingPoint;
|
||||
|
||||
blockCopy->index = originalBlock->index;
|
||||
|
||||
blockCopy->bufferSize = originalBlock->bufferSize;
|
||||
|
||||
blockCopy->createBuffer();
|
||||
|
||||
return blockCopy;
|
||||
|
||||
}
|
||||
|
||||
struct block * getBlock( char * blockName ) {
|
||||
|
||||
int blockCount = this->blocks->length();
|
||||
|
||||
for ( int i = 0; i < blockCount; ++i )
|
||||
{
|
||||
block * currentBlock = this->blocks->get( i );
|
||||
|
||||
char * currentBlockName = currentBlock->name;
|
||||
|
||||
if( currentBlockName == blockName ) {
|
||||
|
||||
return currentBlock;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
create() {
|
||||
|
||||
this->glProgram = glCreateProgram();
|
||||
|
||||
|
||||
array * shaders = this->shaders;
|
||||
|
||||
int shaderCount = shaders->length();
|
||||
|
||||
|
||||
for (int i = 0; i < shaderCount; ++i)
|
||||
{
|
||||
|
||||
shader * currentShader = shaders->get( i );
|
||||
|
||||
glAttachShader( this->glProgram, currentShader->glShader );
|
||||
|
||||
}
|
||||
|
||||
glLinkProgram( this->glProgram );
|
||||
|
||||
glUseProgram( this->glProgram );
|
||||
|
||||
|
||||
this->extractBlocks();
|
||||
|
||||
this->extractAttributes();
|
||||
|
||||
this->extractUniforms();
|
||||
|
||||
}
|
||||
|
||||
bindBlock( char * blockName ) {
|
||||
|
||||
block * currentBlock = this->getBlock( blockName );
|
||||
|
||||
glBindBufferBase( currentBlock->bufferType, currentBlock->bindingPoint, currentBlock->buffer );
|
||||
|
||||
}
|
||||
|
||||
use() {
|
||||
|
||||
glUseProgram( this->glProgram );
|
||||
|
||||
}
|
||||
|
||||
|
||||
extractUniforms() {
|
||||
|
||||
int attributeCount = 0;
|
||||
|
||||
GLsizei bufSize = 64;
|
||||
|
||||
GLsizei length; // name length
|
||||
|
||||
GLint size;
|
||||
|
||||
GLenum type; // type of the variable (float, vec3 or mat4, etc)
|
||||
|
||||
int uniformCount = 0;
|
||||
|
||||
glGetProgramiv( this->glProgram, GL_ACTIVE_UNIFORMS, &uniformCount );
|
||||
|
||||
for (int i = 0; i < uniformCount; i++)
|
||||
{
|
||||
|
||||
struct uniform * uniformInstance = new uniform();
|
||||
|
||||
GLenum type;
|
||||
|
||||
GLchar name[bufSize];
|
||||
|
||||
glGetActiveUniform( this->glProgram, ( GLuint ) i, bufSize, &length, &size, &type, uniformInstance->name );
|
||||
|
||||
//printf( " Uniform #%d Type: %u Name: %s\n", i, type, uniformInstance->name );
|
||||
|
||||
GLint uniformLocation = glGetUniformLocation( this->glProgram, uniformInstance->name );
|
||||
|
||||
uniformInstance->location = uniformLocation;
|
||||
|
||||
uniformInstance->type = type;
|
||||
|
||||
this->uniforms->add( uniformInstance );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extractAttributes() {
|
||||
|
||||
int attributeCount = 0;
|
||||
|
||||
GLsizei bufSize = 64;
|
||||
|
||||
GLsizei length; // name length
|
||||
|
||||
GLint size;
|
||||
|
||||
GLenum type; // type of the variable (float, vec3 or mat4, etc)
|
||||
|
||||
glGetProgramiv( this->glProgram, GL_ACTIVE_ATTRIBUTES, &attributeCount );
|
||||
|
||||
for (int i = 0; i < attributeCount; i++)
|
||||
{
|
||||
GLenum type;
|
||||
|
||||
attribute * attributeInstance = new attribute();
|
||||
|
||||
glGetActiveAttrib( this->glProgram, ( GLuint ) i, bufSize, &length, &size, &type, attributeInstance->name);
|
||||
|
||||
//printf(" Attribute #%d Type: %u Name: %s size: %i\n", i, type, attributeInstance->name, size);
|
||||
|
||||
GLint attributeLocation = glGetAttribLocation( this->glProgram, attributeInstance->name );
|
||||
|
||||
glEnableVertexAttribArray( attributeLocation );
|
||||
|
||||
|
||||
attributeInstance->location = attributeLocation;
|
||||
|
||||
//attributeInstance.name = name;
|
||||
|
||||
attributeInstance->type = type;
|
||||
|
||||
this->attributes->add( attributeInstance );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct attribute * getAttributeByName( char * attributeName ) {
|
||||
|
||||
int attributeCount = this->attributes->length();
|
||||
|
||||
//printf("uniformCount: %i\n\n", attributeCount);
|
||||
|
||||
for ( int i = 0; i < attributeCount; ++i )
|
||||
{
|
||||
struct attribute * currentAttribute = this->attributes->get( i );
|
||||
|
||||
char * currentAttributeName = currentAttribute->name;
|
||||
|
||||
//printf("attributeName->name: %s %s\n\n", currentAttributeName, attributeName );
|
||||
|
||||
if( currentAttributeName == attributeName ) {
|
||||
|
||||
return currentAttribute;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
void setUniform( char * name, void * value ) {
|
||||
|
||||
int uniformCount = this->uniforms->length();
|
||||
|
||||
// printf("uniformCount: %i\n\n", uniformCount);
|
||||
|
||||
for (int i = 0; i < uniformCount; ++i)
|
||||
{
|
||||
uniform * currentUniform = this->uniforms->get( i );
|
||||
|
||||
char * uniformName = (char *)currentUniform->name;
|
||||
|
||||
// printf("currentUniform->name: %s\n\n", uniformName);
|
||||
|
||||
if( uniformName == name ) {
|
||||
|
||||
//printf("\n\n Update this uniform %s\n\n", name);
|
||||
|
||||
switch( currentUniform->type ) {
|
||||
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
vector2 * vector2Value = ( vector2 * ) value;
|
||||
|
||||
//printf("\n\n this is an float vec2 %f %f \n\n", vector2Value->x, vector2Value->y );
|
||||
|
||||
glUniform2f( currentUniform->location, vector2Value->x, vector2Value->y );
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
vector3 * vector3Value = ( vector3 * ) value;
|
||||
|
||||
//printf("\n\n this is an float vec3 %f %f %f \n\n", vector3Value->x, vector3Value->y, vector3Value->z);
|
||||
|
||||
glUniform3f( currentUniform->location, vector3Value->x, vector3Value->y, vector3Value->z );
|
||||
|
||||
break;
|
||||
|
||||
case GL_SAMPLER_2D:
|
||||
|
||||
this->updateSampler2D( currentUniform, value );
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case GL_SAMPLER_2D_ARRAY:
|
||||
|
||||
this->updateSampler2D( currentUniform, value );
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
updateSampler2D( uniform * currentUniform, void * value ) {
|
||||
|
||||
glEnable( GL_BLEND );
|
||||
|
||||
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
|
||||
|
||||
sampler2D * sampler = ( sampler2D * ) value;
|
||||
|
||||
|
||||
if( !sampler->binded ) {
|
||||
|
||||
sampler->index = this->samplerIndex++;
|
||||
|
||||
sampler->bind();
|
||||
|
||||
//printf("sampler->index: %i \n", sampler->index);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
glActiveTexture( GL_TEXTURE0 + sampler->index );
|
||||
|
||||
glBindTexture( sampler->target, sampler->glTexture );
|
||||
|
||||
//glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,GL_UNSIGNED_BYTE, texture );
|
||||
|
||||
glUniform1i( currentUniform->location, sampler->index );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
40
application/source/engine/quadMesh.c
Normal file
40
application/source/engine/quadMesh.c
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
#include "vector2.h"
|
||||
|
||||
#include "vector3.h"
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
|
||||
|
||||
class quadMesh {
|
||||
|
||||
vector2 position;
|
||||
|
||||
vector2 size;
|
||||
|
||||
vector3 color;
|
||||
|
||||
float zIndex;
|
||||
|
||||
float opacity;
|
||||
|
||||
int textureIndex;
|
||||
|
||||
int features;
|
||||
|
||||
int elementIndex;
|
||||
|
||||
|
||||
|
||||
|
||||
// ------------
|
||||
|
||||
//float PADDING;
|
||||
|
||||
// -----------
|
||||
|
||||
//struct vector<int> * characters;
|
||||
|
||||
|
||||
}
|
||||
27
application/source/engine/renderPasses/renderPass.c
Normal file
27
application/source/engine/renderPasses/renderPass.c
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
#include "mesh.h"
|
||||
|
||||
|
||||
class renderPass{
|
||||
|
||||
bool enabled = true;
|
||||
|
||||
struct shader * shader;
|
||||
|
||||
struct mesh * mesh;
|
||||
|
||||
prepare() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
render() {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
96
application/source/engine/renderPasses/renderPassCompute.c
Normal file
96
application/source/engine/renderPasses/renderPassCompute.c
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
|
||||
#include "./renderPass.h"
|
||||
|
||||
#include "../event.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../shader.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../int.h"
|
||||
|
||||
#include "../sampler2D.h"
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "../block.h"
|
||||
|
||||
#include "../vector.h"
|
||||
|
||||
#include "../mesh.h"
|
||||
|
||||
|
||||
class compute extends renderPass{
|
||||
|
||||
|
||||
struct program * program;
|
||||
|
||||
int active = true;
|
||||
|
||||
prepare() {
|
||||
|
||||
printf("\n\n\n Prepare renderPass Compute\n\n\n\n\n");
|
||||
|
||||
shader * computeShader = new shader( GL_COMPUTE_SHADER );
|
||||
|
||||
computeShader->loadFromFile( "assets/shaders/addition.comp" );
|
||||
|
||||
|
||||
this->program = new program();
|
||||
|
||||
this->program->addShader( computeShader );
|
||||
|
||||
this->program->create();
|
||||
|
||||
|
||||
vector<vector2> * inputA = new vector();
|
||||
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
|
||||
vector2 a = new vector2( i, i );
|
||||
|
||||
inputA->add( a );
|
||||
|
||||
}
|
||||
|
||||
vector<vector2> * inputB = new vector();
|
||||
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
|
||||
vector2 a = new vector2( 0, 10 );
|
||||
|
||||
inputB->add( a );
|
||||
|
||||
}
|
||||
|
||||
block * inputBlock = this->program->getBlock( "inputBlock" );
|
||||
|
||||
inputBlock->setMemberArray( "array_a[0]", ( float * ) inputA->items );
|
||||
|
||||
inputBlock->setMemberArray( "array_b[0]", ( float * ) inputB->items );
|
||||
|
||||
inputBlock->upload();
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
this->program->use();
|
||||
|
||||
this->program->bindBlock( "inputBlock");
|
||||
|
||||
this->program->bindBlock( "outputBlock");
|
||||
|
||||
|
||||
glDispatchCompute( 1, 1, 1 );
|
||||
|
||||
glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
80
application/source/engine/renderPasses/renderPassCompute2.c
Normal file
80
application/source/engine/renderPasses/renderPassCompute2.c
Normal file
@@ -0,0 +1,80 @@
|
||||
|
||||
#include "renderPass.h"
|
||||
|
||||
#include "../event.h"
|
||||
|
||||
#include "../../vector2.h"
|
||||
|
||||
#include "../shader.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../../int.h"
|
||||
|
||||
#include "../sampler2D.h"
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "../block.h"
|
||||
|
||||
|
||||
class compute2 extends renderPass{
|
||||
|
||||
struct program * program;
|
||||
|
||||
int active = true;
|
||||
|
||||
|
||||
prepare() {
|
||||
|
||||
printf("\n\n\n Prepare renderPass Compute 2\n\n\n\n\n");
|
||||
|
||||
shader * computeShader = new shader( GL_COMPUTE_SHADER );
|
||||
|
||||
computeShader->loadFromFile( "assets/shaders/addition2.comp" );
|
||||
|
||||
|
||||
this->program = new program();
|
||||
|
||||
this->program->addShader( computeShader );
|
||||
|
||||
this->program->create();
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
if( this->active ) {
|
||||
|
||||
this->program->use();
|
||||
|
||||
this->program->bindBlock( "outputBlock2" );
|
||||
|
||||
|
||||
glDispatchCompute( 1, 1, 1 );
|
||||
|
||||
|
||||
block * outputBlock = this->program->getBlock( "outputBlock2" );
|
||||
|
||||
vector<vector2> * output = outputBlock->getMemberArray( "array_d[0]" );
|
||||
|
||||
int count = output->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
vector2 currentVector = output->get( i );
|
||||
|
||||
printf("%i = %f %f \n", i, i, currentVector.x, currentVector.y );
|
||||
|
||||
}
|
||||
|
||||
printf("length: %i\n\n", count);
|
||||
|
||||
this->active = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
174
application/source/engine/renderPasses/renderPassFont.c
Normal file
174
application/source/engine/renderPasses/renderPassFont.c
Normal file
@@ -0,0 +1,174 @@
|
||||
|
||||
|
||||
#include "renderPass.h"
|
||||
|
||||
#include "../event.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../shader.h"
|
||||
|
||||
#include "int.h"
|
||||
|
||||
#include "../fontRenderer.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../mesh.h"
|
||||
|
||||
class font extends renderPass{
|
||||
|
||||
|
||||
struct program * program;
|
||||
|
||||
struct mesh * mesh;
|
||||
|
||||
fontRenderer * font = new fontRenderer();
|
||||
|
||||
sampler2D * samplerArray;
|
||||
|
||||
char * textFromNumber( int i) {
|
||||
|
||||
char * fileName = malloc( sizeof( char ) * 100 );
|
||||
|
||||
sprintf( fileName, "%d", i );
|
||||
|
||||
return fileName;
|
||||
|
||||
}
|
||||
|
||||
prepare() {
|
||||
|
||||
printf("\n\n\n Prepare renderPass Font\n\n\n\n\n");
|
||||
|
||||
|
||||
|
||||
|
||||
shader * vertexShader = new shader( GL_VERTEX_SHADER );
|
||||
|
||||
vertexShader->loadFromFile( "assets/shaders/quad.vertex" );
|
||||
|
||||
|
||||
shader * fragmentShader = new shader( GL_FRAGMENT_SHADER );
|
||||
|
||||
fragmentShader->loadFromFile( "assets/shaders/quad.fragment" );
|
||||
|
||||
|
||||
|
||||
this->program = new program();
|
||||
|
||||
this->program->addShader( vertexShader );
|
||||
|
||||
this->program->addShader( fragmentShader );
|
||||
|
||||
this->program->create();
|
||||
|
||||
|
||||
sampler2D * samplerArray = new sampler2D();
|
||||
|
||||
samplerArray->target = GL_TEXTURE_2D_ARRAY;
|
||||
|
||||
samplerArray->format = GL_RED;
|
||||
|
||||
samplerArray->internalFormat = GL_RED;
|
||||
|
||||
samplerArray->WRAP_S = GL_CLAMP_TO_EDGE;
|
||||
|
||||
samplerArray->WRAP_S = GL_CLAMP_TO_EDGE;
|
||||
|
||||
samplerArray->cubeSize = new vector3( 128, 128, 170 );
|
||||
|
||||
samplerArray->UNPACK_ALIGNMENT = true;
|
||||
|
||||
|
||||
this->samplerArray = samplerArray;
|
||||
|
||||
block * fontBlock = this->program->getBlock( "fontData" );
|
||||
|
||||
|
||||
this->mesh = new mesh();
|
||||
|
||||
this->mesh->setProgram( this->program );
|
||||
|
||||
this->mesh->createBuffers();
|
||||
|
||||
|
||||
glUseProgram( this->program->glProgram );
|
||||
|
||||
for ( int i = 1; i < 170; ++i )
|
||||
{
|
||||
texture2D * characterTexture = this->font->loadFont( 34 + i );
|
||||
|
||||
vector2 * offset = characterTexture->offset;
|
||||
|
||||
printf("%c offset left: %f, offset top: %f bitmap->rows: %i\n", (char) 34 + i, offset->x, offset->y, characterTexture->height );
|
||||
|
||||
vector2 * size = new vector2( characterTexture->width, characterTexture->height );
|
||||
|
||||
fontBlock->setMemberItem( "fontOffsets[0]", i - 35, offset );
|
||||
|
||||
//if( i > 34 ) {
|
||||
|
||||
fontBlock->setMemberItem( "fontSizes[0]", i - 35, size );
|
||||
|
||||
//}
|
||||
|
||||
samplerArray->addTexture( characterTexture );
|
||||
|
||||
}
|
||||
|
||||
fontBlock->upload();
|
||||
|
||||
this->program->setUniform( "samplerArray", samplerArray );
|
||||
|
||||
|
||||
|
||||
vector<int> * textArray = new vector();
|
||||
|
||||
char * someText = "Wauw this is myp first text.";
|
||||
|
||||
for (int i = 0; i < strlen(someText); ++i)
|
||||
{
|
||||
|
||||
int charNumber = ( char ) someText[i] - 35;
|
||||
|
||||
printf(" %i\n", charNumber );
|
||||
|
||||
textArray->add( charNumber );//
|
||||
|
||||
}
|
||||
|
||||
block * inputBlock = this->program->getBlock( "inputBlock" );
|
||||
|
||||
inputBlock->setMemberArray( "characters[0]", ( float * ) textArray->items );
|
||||
|
||||
inputBlock->upload();
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
int numItems = 12;
|
||||
|
||||
|
||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
|
||||
this->program->use();
|
||||
|
||||
this->program->bindBlock( "inputBlock");
|
||||
|
||||
this->program->bindBlock( "fontData");
|
||||
|
||||
|
||||
this->program->setUniform( "samplerArray", this->samplerArray );
|
||||
|
||||
|
||||
|
||||
glBindVertexArray( this->mesh->vertexArrayObject );
|
||||
|
||||
glDrawElements( GL_TRIANGLES, numItems, GL_UNSIGNED_INT, ( void * ) 0 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
700
application/source/engine/renderPasses/renderPassQuads.c
Normal file
700
application/source/engine/renderPasses/renderPassQuads.c
Normal file
@@ -0,0 +1,700 @@
|
||||
|
||||
#include "renderPass.h"
|
||||
|
||||
#include "../event.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../vector3.h"
|
||||
|
||||
#include "../vector4.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../shader.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../../int.h"
|
||||
|
||||
#include "../fontRenderer.h"
|
||||
|
||||
#include "../resourceManager.h"
|
||||
|
||||
#include "../mesh.h"
|
||||
|
||||
#include "../quadMesh.h"
|
||||
|
||||
#include "../element.h"
|
||||
|
||||
|
||||
|
||||
|
||||
extern event * globalEvent;
|
||||
|
||||
extern resourceManager * resources;
|
||||
|
||||
|
||||
|
||||
class quads extends renderPass{
|
||||
|
||||
mesh * mesh;
|
||||
|
||||
fontRenderer * font = new fontRenderer();
|
||||
|
||||
sampler2D * samplerArray;
|
||||
|
||||
program * program;
|
||||
|
||||
vector<quadMesh> * meshes = new vector();
|
||||
|
||||
vector<element> * elements = new vector();
|
||||
|
||||
vector<int> * mouseOverElements = new vector();
|
||||
|
||||
char * textFromNumber( int i) {
|
||||
|
||||
char * fileName = malloc( sizeof( char ) * 100 );
|
||||
|
||||
sprintf( fileName, "%d", i );
|
||||
|
||||
return fileName;
|
||||
|
||||
}
|
||||
|
||||
prepare() {
|
||||
|
||||
printf("\n\n\n Prepare renderPass Quad\n\n\n\n\n");
|
||||
|
||||
/*
|
||||
sampler2D * sampler1 = new sampler2D();
|
||||
|
||||
sampler1->texture = resources->loadPngImage( "logo.png" );
|
||||
|
||||
*/
|
||||
|
||||
this->samplerArray = new sampler2D();
|
||||
|
||||
|
||||
for (int i = 1; i < 10; ++i )
|
||||
{
|
||||
|
||||
char * fileName = this->textFromNumber( i );
|
||||
|
||||
fileName += ".png";
|
||||
|
||||
printf("load png: %s\n", fileName);
|
||||
|
||||
//this->samplerArray->addTexture( resources->loadPngImage( fileName ) );
|
||||
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
this->samplerArray->target = GL_TEXTURE_2D_ARRAY;
|
||||
|
||||
|
||||
shader * vertexShader = new shader( GL_VERTEX_SHADER );
|
||||
|
||||
vertexShader->loadFromFile( "assets/shaders/multiQuad.vertex" );
|
||||
|
||||
|
||||
shader * fragmentShader = new shader( GL_FRAGMENT_SHADER );
|
||||
|
||||
fragmentShader->loadFromFile( "assets/shaders/multiQuad.fragment" );
|
||||
|
||||
|
||||
this->program = new program();
|
||||
|
||||
this->program->addShader( vertexShader );
|
||||
|
||||
this->program->addShader( fragmentShader );
|
||||
|
||||
this->program->create();
|
||||
|
||||
|
||||
|
||||
//block * eventsBlock = this->program->getBlock( "events" );
|
||||
|
||||
block * orientationBlock = this->program->getBlock( "orientation" );
|
||||
|
||||
block * meshesBlock = this->program->getBlock( "meshes" );
|
||||
|
||||
|
||||
this->mesh = new mesh();
|
||||
|
||||
this->mesh->setProgram( this->program );
|
||||
|
||||
this->mesh->createBuffers();
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
for (int vertexIndex = 0; vertexIndex < 100; ++vertexIndex)
|
||||
{
|
||||
|
||||
int mod = vertexIndex % 10;
|
||||
|
||||
vector2 test = new vector2( mod * 0.1 , floor( vertexIndex / 10 ) * 0.1 );
|
||||
|
||||
meshesBlock->setMemberItem( "meshArray[0].position", vertexIndex, &test );
|
||||
|
||||
|
||||
vector2 size = new vector2( .05 , .05 );
|
||||
|
||||
meshesBlock->setMemberItem( "meshArray[0].size", vertexIndex, &size );
|
||||
|
||||
vector3 color = new vector3( (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
|
||||
|
||||
|
||||
printf("Random: %f %f %f\n", color.x, color.y, color.z);
|
||||
|
||||
meshesBlock->setMemberItem( "meshArray[0].color", vertexIndex, &color );
|
||||
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
this->createMeshes();
|
||||
|
||||
//this->program->setUniform( "samplerArray", this->samplerArray );
|
||||
|
||||
|
||||
this->samplerArray->addTexture( resources->loadPngImage( "1.png" ) );
|
||||
|
||||
|
||||
meshesBlock->upload();
|
||||
|
||||
}
|
||||
|
||||
sortOpacity( struct vector< struct quadMesh > * meshes ) {
|
||||
|
||||
|
||||
int count = meshes->length();
|
||||
|
||||
int i, j;
|
||||
|
||||
struct quadMesh temp;
|
||||
|
||||
for (i = 0; i < (count - 1); ++i)
|
||||
{
|
||||
for (j = 0; j < count - 1 - i; ++j )
|
||||
{
|
||||
|
||||
struct quadMesh quadA = meshes->get( j );
|
||||
|
||||
struct quadMesh quadB = meshes->get( j + 1 );
|
||||
|
||||
float a = ( intptr_t ) quadA.zIndex;
|
||||
|
||||
float b = ( intptr_t ) quadB.zIndex;
|
||||
|
||||
if ( a > b )
|
||||
{
|
||||
|
||||
temp = meshes->items[j+1];
|
||||
|
||||
meshes->items[ j + 1 ] = meshes->items[j];
|
||||
|
||||
meshes->items[ j ] = temp;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createMeshes() {
|
||||
|
||||
|
||||
vector<quadMesh> * meshes = this->meshes;
|
||||
|
||||
meshes->resize( 100 );
|
||||
|
||||
/*
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
|
||||
int mod = i % 10;
|
||||
|
||||
quadMesh meshInstance = new quadMesh();
|
||||
|
||||
meshInstance.position = new vector2( mod * 0.1 , floor( i / 10 ) * 0.1 );
|
||||
|
||||
meshInstance.size = new vector2( .05 , .05 );
|
||||
|
||||
meshInstance.color = new vector3( (float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX);
|
||||
|
||||
// meshInstance.textureIndex = 0;
|
||||
|
||||
meshes->add( meshInstance );
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
vector<element> * elements = this->elements;
|
||||
|
||||
|
||||
{
|
||||
|
||||
element instance = new element();
|
||||
|
||||
instance.position = new vector3( 200, 200, 1100 );
|
||||
|
||||
instance.size = new vector2( 200., 200. );
|
||||
|
||||
//instance.background = new vector3( 0, 256, 0 );
|
||||
|
||||
instance.opacity = 1;
|
||||
|
||||
instance.background = "9.png";
|
||||
|
||||
//instance.background = new vector3( 0, 256, 0 );
|
||||
|
||||
elements->add( instance );
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
|
||||
element instance = new element();
|
||||
|
||||
instance.position = new vector3( 100, 100, 500 );
|
||||
|
||||
instance.size = new vector2( 400., 400. );
|
||||
|
||||
instance.background = "3.png";
|
||||
|
||||
//instance.background = "#2196f354";
|
||||
|
||||
//instance.background = new vector3( 0, 0, 256 );
|
||||
|
||||
instance.opacity = .9;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
instance.addEventListener( "click", void clickEvent( event * e ) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} );
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
//instance.background = new vector2( 100, 200 );
|
||||
|
||||
//instance.background = new vector3( 100, 200, 300 );
|
||||
|
||||
// instance.background = new vector4( 100, 200, 300, 400 );
|
||||
|
||||
//instance.background = "something/something.png";
|
||||
|
||||
elements->add( instance );
|
||||
|
||||
|
||||
|
||||
{
|
||||
|
||||
element instance = new element();
|
||||
|
||||
instance.position = new vector3( 20, 0, 1300 );
|
||||
|
||||
instance.size = new vector2( 40., 40. );
|
||||
|
||||
instance.background = "7.png";
|
||||
|
||||
instance.opacity = 1;
|
||||
|
||||
elements->add( instance );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int count = elements->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
element currentElement = elements->get( i );
|
||||
|
||||
quadMesh meshInstance = new quadMesh();
|
||||
|
||||
|
||||
meshInstance.elementIndex = i;
|
||||
|
||||
meshInstance.position = new vector2( currentElement.position.x, currentElement.position.y );
|
||||
|
||||
meshInstance.zIndex = currentElement.position.z;
|
||||
|
||||
meshInstance.size = currentElement.size;
|
||||
|
||||
|
||||
printf("zIndex: %f\n", currentElement.position.z);
|
||||
|
||||
int features = 0;
|
||||
|
||||
if( currentElement.useBackgroundImage ) {
|
||||
|
||||
|
||||
//int featureValue = currentElement->getFeatureValueByFeatureName("useBackgroundImage");
|
||||
|
||||
|
||||
meshInstance.textureIndex = this->samplerArray->getTextureIndex();
|
||||
|
||||
printf("use background image. %s \n", currentElement.backgroundImagePath );
|
||||
|
||||
this->samplerArray->addTexture( resources->loadPngImage( currentElement.backgroundImagePath ) );
|
||||
|
||||
} else {
|
||||
|
||||
printf("dont use background color. %f %f %f \n", currentElement.backgroundColor.x, currentElement.backgroundColor.y, currentElement.backgroundColor.z);
|
||||
|
||||
meshInstance.color = currentElement.backgroundColor;
|
||||
|
||||
}
|
||||
|
||||
|
||||
meshInstance.features = currentElement.updateFeature();
|
||||
|
||||
meshInstance.opacity = currentElement.opacity;
|
||||
|
||||
//printf("value: %i\n", value);
|
||||
|
||||
|
||||
currentElement.setOriginal();
|
||||
|
||||
elements->set( i, currentElement );
|
||||
|
||||
|
||||
meshes->add( meshInstance );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
this->sortOpacity( meshes );
|
||||
|
||||
|
||||
|
||||
}
|
||||
/*
|
||||
|
||||
|
||||
{
|
||||
quadMesh meshInstance = new quadMesh();
|
||||
|
||||
meshInstance.position = new vector2( 0. , 0. );
|
||||
|
||||
meshInstance.size = new vector2( 50. , 50. );
|
||||
|
||||
meshInstance.color = new vector3( 0.0, 1.0, 1.0 );
|
||||
|
||||
|
||||
meshes->add( meshInstance );
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
quadMesh meshInstance = new quadMesh();
|
||||
|
||||
meshInstance.position = new vector2( 50. , 50. );
|
||||
|
||||
meshInstance.size = new vector2( 100. , 100. );
|
||||
|
||||
meshInstance.color = new vector3( 0.0, 0.0, 1.0 );
|
||||
|
||||
|
||||
meshes->add( meshInstance );
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
quadMesh meshInstance = new quadMesh();
|
||||
|
||||
meshInstance.position = new vector2( 150. , 150. );
|
||||
|
||||
meshInstance.size = new vector2( 50. , 50. );
|
||||
|
||||
meshInstance.color = new vector3( 1.0, 0.0, 1.0 );
|
||||
|
||||
|
||||
meshes->add( meshInstance );
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
block * meshesBlock = this->program->getBlock( "meshes" );
|
||||
|
||||
meshesBlock->setData( ( float * ) meshes->items );
|
||||
|
||||
}
|
||||
|
||||
|
||||
quadMesh updateMesh( quadMesh currentMesh, element currentElement ) {
|
||||
|
||||
vector2 position = new vector2( currentElement.position.x, currentElement.position.y );
|
||||
|
||||
currentMesh.position = position;
|
||||
|
||||
currentMesh.zIndex = currentElement.position.z;
|
||||
|
||||
currentMesh.size = currentElement.size;
|
||||
|
||||
currentMesh.color = currentElement.backgroundColor;
|
||||
|
||||
return currentMesh;
|
||||
|
||||
}
|
||||
|
||||
void callElementEvents( event * currentEvent, element * currentElement, int elementIndex ) {
|
||||
|
||||
vector<char *> * mouseEvents = currentEvent->mouse->eventTypes;
|
||||
|
||||
int mouseEventCount = mouseEvents->length();
|
||||
|
||||
for (int k = 0; k < mouseEventCount; ++k)
|
||||
{
|
||||
char * mouseEventCode = mouseEvents->get( k );
|
||||
|
||||
printf(" mouse event: %s\n", mouseEventCode);
|
||||
|
||||
|
||||
if( mouseEventCode == "click" ) {
|
||||
|
||||
currentElement->click();
|
||||
|
||||
//printf("trigger click event\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( mouseEventCode == "mousedown" ) {
|
||||
|
||||
currentElement->mousedown();
|
||||
|
||||
//printf("trigger mousedown event\n\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( mouseEventCode == "mouseup" ) {
|
||||
|
||||
currentElement->mouseup();
|
||||
|
||||
//printf("trigger mouseup event\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( mouseEventCode == "mousemove" ) {
|
||||
|
||||
if( !this->integerContains( this->mouseOverElements, elementIndex ) ) {
|
||||
|
||||
currentElement->mouseover();
|
||||
|
||||
this->mouseOverElements->add( elementIndex );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool integerContains( vector<int> * numbers, int a ) {
|
||||
|
||||
int count = numbers->length();
|
||||
|
||||
for (int j = 0; j < count; ++j)
|
||||
{
|
||||
|
||||
int b = numbers->get( j );
|
||||
|
||||
if( a == b ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
program * currentProgram = this->program;
|
||||
|
||||
event * currentEvent = globalEvent;
|
||||
|
||||
|
||||
block * eventsBlock = currentProgram->getBlock( "events" );
|
||||
|
||||
//block * orientationBlock = currentProgram->getBlock( "orientation" );
|
||||
|
||||
|
||||
block * meshesBlock = currentProgram->getBlock( "meshes" );
|
||||
|
||||
|
||||
eventsBlock->autoUpload = true;
|
||||
|
||||
eventsBlock->setMember( "window", ¤tEvent->screen->size );
|
||||
|
||||
|
||||
meshesBlock->autoUpload = true;
|
||||
|
||||
vector2 mousePosition = globalEvent->mouse->position;
|
||||
|
||||
|
||||
int mouseX = ( int ) mousePosition.x;
|
||||
|
||||
int mouseY = ( int ) mousePosition.y;
|
||||
|
||||
|
||||
vector<element> * elements = this->elements;
|
||||
|
||||
vector<quadMesh> * meshes = this->meshes;
|
||||
|
||||
|
||||
vector<int> * mouseOverElements = this->mouseOverElements;
|
||||
|
||||
int count = meshes->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
quadMesh currentMesh = meshes->get( i );
|
||||
|
||||
int left = currentMesh.position.x;
|
||||
|
||||
int top = currentMesh.position.y;
|
||||
|
||||
int right = currentMesh.position.x + currentMesh.size.x;
|
||||
|
||||
int bottom = currentMesh.position.y + currentMesh.size.y;
|
||||
|
||||
int elementIndex = currentMesh.elementIndex;
|
||||
|
||||
|
||||
if( mouseX > left && mouseX < right && mouseY > top && mouseY < bottom ) {
|
||||
|
||||
element currentElement = elements->get( elementIndex );
|
||||
|
||||
this->callElementEvents( currentEvent, ¤tElement, elementIndex );
|
||||
|
||||
currentMesh = this->updateMesh( currentMesh, currentElement );
|
||||
|
||||
elements->set( elementIndex, currentElement );
|
||||
|
||||
meshesBlock->setMemberArrayRow( "meshArray[0].color", i, ( float * ) & currentMesh );
|
||||
|
||||
} else {
|
||||
|
||||
if( this->integerContains( this->mouseOverElements, elementIndex ) ) {
|
||||
|
||||
printf("mouseout\n\n");
|
||||
|
||||
this->mouseOverElements->delete( elementIndex );
|
||||
|
||||
element currentElement = elements->get( elementIndex );
|
||||
|
||||
|
||||
currentElement->mouseleave();
|
||||
|
||||
|
||||
|
||||
currentMesh = this->updateMesh( currentMesh, currentElement );
|
||||
|
||||
elements->set( elementIndex, currentElement );
|
||||
|
||||
meshesBlock->setMemberArrayRow( "meshArray[0].color", i, ( float * ) & currentMesh );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//eventsBlock->setMember( "mouse", currentEvent->mouse->position );
|
||||
|
||||
|
||||
//vector2 * position = new vector2( 0.0, 0.0 );
|
||||
|
||||
//orientationBlock->setMember( "quadPosition", position );
|
||||
|
||||
// meshesBlock->upload();
|
||||
|
||||
|
||||
vector2 * position2 = new vector2( 0.4, 0 );
|
||||
|
||||
//meshesBlock->upload();
|
||||
|
||||
|
||||
|
||||
this->program->setUniform( "samplerArray", this->samplerArray );
|
||||
|
||||
glUseProgram( currentProgram->glProgram );
|
||||
|
||||
int numItems = 200;
|
||||
|
||||
|
||||
|
||||
/* Depth buffer setup */
|
||||
//glClearDepth(1.0f);
|
||||
|
||||
/* Enables Depth Testing */
|
||||
//glDisable(GL_CULL_FACE);
|
||||
|
||||
//glClear(GL_DEPTH_BUFFER_BIT)
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
|
||||
|
||||
//glDepthMask(GL_TRUE);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
//glDepthMask(GL_FALSE);
|
||||
|
||||
|
||||
glDepthRange(0.0, 1.0);
|
||||
|
||||
glDepthFunc(GL_ALWAYS);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
/* The Type Of Depth Test To Do */
|
||||
//glDepthFunc( GL_LEQUAL );
|
||||
|
||||
|
||||
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
|
||||
|
||||
|
||||
|
||||
glBindVertexArray( this->mesh->vertexArrayObject );
|
||||
|
||||
glDrawElements( GL_TRIANGLES, numItems, GL_UNSIGNED_INT, ( void * ) 0 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
118
application/source/engine/renderPasses/renderPassTesselation.c
Normal file
118
application/source/engine/renderPasses/renderPassTesselation.c
Normal file
@@ -0,0 +1,118 @@
|
||||
|
||||
|
||||
#include "renderPass.h"
|
||||
|
||||
#include "../event.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../shader.h"
|
||||
|
||||
#include "int.h"
|
||||
|
||||
#include "../fontRenderer.h"
|
||||
|
||||
#include "../program.h"
|
||||
|
||||
#include "../mesh.h"
|
||||
|
||||
class tesselation extends renderPass{
|
||||
|
||||
|
||||
struct program * program;
|
||||
|
||||
struct mesh * mesh;
|
||||
|
||||
fontRenderer * font = new fontRenderer();
|
||||
|
||||
sampler2D * samplerArray;
|
||||
|
||||
char * textFromNumber( int i) {
|
||||
|
||||
char * fileName = malloc( sizeof( char ) * 100 );
|
||||
|
||||
sprintf( fileName, "%d", i );
|
||||
|
||||
return fileName;
|
||||
|
||||
}
|
||||
|
||||
prepare() {
|
||||
|
||||
shader * vertexShader = new shader( GL_VERTEX_SHADER );
|
||||
|
||||
vertexShader->loadFromFile( "assets/shaders/quadScale.vertex" );
|
||||
|
||||
|
||||
shader * fragmentShader = new shader( GL_FRAGMENT_SHADER );
|
||||
|
||||
fragmentShader->loadFromFile( "assets/shaders/color.fragment" );
|
||||
|
||||
|
||||
shader * geometryShader = new shader( GL_GEOMETRY_SHADER );
|
||||
|
||||
geometryShader->loadFromFile( "assets/shaders/tesselation.geometry.shader" );
|
||||
|
||||
|
||||
shader * tesselationControlShader = new shader( GL_TESS_CONTROL_SHADER );
|
||||
|
||||
tesselationControlShader->loadFromFile( "assets/shaders/tesselation.triangle.tsc.shader" );
|
||||
|
||||
|
||||
shader * tesselationEvaluationShader = new shader( GL_TESS_EVALUATION_SHADER );
|
||||
|
||||
tesselationEvaluationShader->loadFromFile( "assets/shaders/tesselation.triangle.shader" );
|
||||
|
||||
|
||||
|
||||
this->program = new program();
|
||||
|
||||
this->program->addShader( vertexShader );
|
||||
|
||||
this->program->addShader( fragmentShader );/*
|
||||
|
||||
this->program->addShader( geometryShader );
|
||||
|
||||
this->program->addShader( tesselationControlShader );
|
||||
|
||||
this->program->addShader( tesselationEvaluationShader );*/
|
||||
|
||||
|
||||
|
||||
this->program->create();
|
||||
|
||||
|
||||
|
||||
|
||||
this->mesh = new mesh();
|
||||
|
||||
this->mesh->setProgram( this->program );
|
||||
|
||||
this->mesh->createBuffers();
|
||||
|
||||
|
||||
glUseProgram( this->program->glProgram );
|
||||
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
glUseProgram( this->program->glProgram );
|
||||
|
||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
int numItems = 12;
|
||||
|
||||
//this->program->setUniform( "samplerArray", this->samplerArray );
|
||||
|
||||
glBindVertexArray( this->mesh->vertexArrayObject );
|
||||
|
||||
|
||||
|
||||
|
||||
glDrawElements( GL_LINE_STRIP, numItems, GL_UNSIGNED_INT, ( void * ) 0 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
112
application/source/engine/resourceManager.c
Normal file
112
application/source/engine/resourceManager.c
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
|
||||
#include "texture2D.h"
|
||||
|
||||
#include <png.h>
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "string.h"
|
||||
|
||||
class resourceManager{
|
||||
|
||||
texture2D * loadPngImage( char * name ) {
|
||||
|
||||
texture2D * texture = new texture2D();
|
||||
|
||||
png_structp png_ptr;
|
||||
|
||||
png_infop info_ptr;
|
||||
|
||||
unsigned int sig_read = 0;
|
||||
|
||||
int color_type;
|
||||
|
||||
int interlace_type;
|
||||
|
||||
FILE * fp;
|
||||
|
||||
if ( ( fp = fopen( name, "rb" ) ) == NULL ) {
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
|
||||
|
||||
if ( png_ptr == NULL ) {
|
||||
|
||||
fclose( fp );
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
info_ptr = png_create_info_struct( png_ptr );
|
||||
|
||||
if ( info_ptr == NULL ) {
|
||||
|
||||
fclose(fp);
|
||||
|
||||
png_destroy_read_struct( &png_ptr, NULL, NULL );
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
|
||||
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
|
||||
|
||||
fclose( fp );
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
png_init_io( png_ptr, fp );
|
||||
|
||||
png_set_sig_bytes( png_ptr, sig_read );
|
||||
|
||||
png_read_png( png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL );
|
||||
|
||||
png_uint_32 width, height;
|
||||
|
||||
int bit_depth;
|
||||
|
||||
png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL );
|
||||
|
||||
unsigned int row_bytes = png_get_rowbytes( png_ptr, info_ptr );
|
||||
|
||||
|
||||
|
||||
texture->width = width;
|
||||
|
||||
texture->height = height;
|
||||
|
||||
texture->hasAlpha = ( color_type == PNG_COLOR_TYPE_RGBA );
|
||||
|
||||
texture->data = ( unsigned char * ) malloc( row_bytes * texture->height );
|
||||
|
||||
|
||||
png_bytepp row_pointers = png_get_rows( png_ptr, info_ptr );
|
||||
|
||||
for (int i = 0; i < texture->height; i++) {
|
||||
|
||||
// note that png is ordered top to
|
||||
// bottom, but OpenGL expect it bottom to top
|
||||
// so the order or swapped
|
||||
|
||||
memcpy( texture->data + ( row_bytes * ( texture->height - 1 - i ) ), row_pointers[ i ], row_bytes );
|
||||
|
||||
}
|
||||
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
|
||||
|
||||
fclose( fp );
|
||||
|
||||
return texture;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
208
application/source/engine/sampler2D.c
Normal file
208
application/source/engine/sampler2D.c
Normal file
@@ -0,0 +1,208 @@
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include "./texture2D.h"
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "../vector3.h"
|
||||
|
||||
class sampler2D{
|
||||
|
||||
texture2D * texture;
|
||||
|
||||
array * textures = new array();
|
||||
|
||||
|
||||
GLuint glTexture;
|
||||
|
||||
|
||||
GLint binded = false;
|
||||
|
||||
|
||||
GLint filter = GL_LINEAR;
|
||||
|
||||
|
||||
GLint MIN_FILTER = GL_LINEAR;
|
||||
|
||||
GLint MAG_FILTER = GL_LINEAR;
|
||||
|
||||
GLint WRAP_S = GL_REPEAT;
|
||||
|
||||
GLint WRAP_T = GL_REPEAT;
|
||||
|
||||
|
||||
GLint datatype = GL_RGBA;
|
||||
|
||||
GLint format = GL_RGBA;
|
||||
|
||||
GLint internalFormat = GL_RGBA;
|
||||
|
||||
|
||||
GLint target = GL_TEXTURE_2D;
|
||||
|
||||
GLint type = GL_UNSIGNED_BYTE;//gl.FLOAT;
|
||||
|
||||
|
||||
vector3 * cubeSize = NULL;
|
||||
|
||||
|
||||
GLint border = false;
|
||||
|
||||
GLint generateMipmap = true;
|
||||
|
||||
bool UNPACK_ALIGNMENT = false;
|
||||
|
||||
//GLint FLIP_Y = true; pixelStorei
|
||||
|
||||
GLuint index = 0;
|
||||
|
||||
constructor() {
|
||||
|
||||
glGenTextures( 1, &this->glTexture );
|
||||
|
||||
}
|
||||
|
||||
addTexture( texture2D * texture ) {
|
||||
|
||||
this->textures->add( texture );
|
||||
|
||||
}
|
||||
|
||||
int getTextureIndex() {
|
||||
|
||||
int numberOfTextures = this->textures->length();
|
||||
|
||||
return numberOfTextures;
|
||||
|
||||
}
|
||||
|
||||
bind() {
|
||||
|
||||
this->binded = true;
|
||||
/*
|
||||
texture2D * texture = this->texture;
|
||||
|
||||
if( texture == NULL ) {
|
||||
|
||||
printf("Error: Texture not loaded.\n\n");
|
||||
|
||||
return ;
|
||||
|
||||
}
|
||||
*/
|
||||
//printf("Created sampler with id #%i\n", this->index);
|
||||
|
||||
glActiveTexture( GL_TEXTURE0 + this->index );
|
||||
|
||||
glBindTexture( this->target, this->glTexture );
|
||||
|
||||
glTexParameteri( this->target, GL_TEXTURE_WRAP_S, this->WRAP_S );
|
||||
|
||||
glTexParameteri( this->target, GL_TEXTURE_WRAP_T, this->WRAP_T );
|
||||
|
||||
glTexParameteri( this->target, GL_TEXTURE_MIN_FILTER, this->MIN_FILTER );
|
||||
|
||||
glTexParameteri( this->target, GL_TEXTURE_MAG_FILTER, this->MAG_FILTER );
|
||||
|
||||
|
||||
|
||||
if( this->target == GL_TEXTURE_2D_ARRAY ) {
|
||||
|
||||
//printf("create 2d array texture");
|
||||
|
||||
int offsetX = 0;
|
||||
|
||||
int offsetY = 0;
|
||||
|
||||
int offsetZ = 0;
|
||||
|
||||
int depth = 1;
|
||||
|
||||
int levelOfDetail = 0;
|
||||
|
||||
int layerCount = 2;
|
||||
|
||||
int mipLevelCount = 1;
|
||||
|
||||
int currentLayer = 0;
|
||||
|
||||
texture2D * texture1 = this->textures->get( 0 );
|
||||
|
||||
int numberOfTextures = this->textures->length();
|
||||
|
||||
|
||||
if( this->cubeSize == NULL ) {
|
||||
|
||||
this->cubeSize = new vector3( texture1->width, texture1->height, numberOfTextures );
|
||||
|
||||
}
|
||||
|
||||
//glTexSubImage3D( this->target, levelOfDetail, offsetX, offsetY, offsetZ, texture->width, texture->height, depth, this->format, this->type, texture->data );
|
||||
|
||||
if( this->UNPACK_ALIGNMENT ) {
|
||||
|
||||
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
|
||||
|
||||
} else {
|
||||
|
||||
glPixelStorei( GL_UNPACK_ALIGNMENT, 0 );
|
||||
|
||||
}
|
||||
|
||||
glTexStorage3D( this->target, mipLevelCount, this->format, this->cubeSize->x, this->cubeSize->y, numberOfTextures );
|
||||
|
||||
GLint data[ texture1->width * texture1->height * numberOfTextures ];
|
||||
|
||||
glTexImage3D( GL_TEXTURE_2D_ARRAY, levelOfDetail, this->internalFormat, this->cubeSize->x, this->cubeSize->y, numberOfTextures, this->border, this->format, this->type, data);
|
||||
|
||||
for (int i = 0; i < numberOfTextures; ++i)
|
||||
{
|
||||
|
||||
texture2D * currentTexture = this->textures->get( i );
|
||||
|
||||
float test = 0;//this->cubeSize->y - currentTexture->height - 1;
|
||||
|
||||
glTexSubImage3D( this->target,
|
||||
0,
|
||||
offsetX, test , i,
|
||||
currentTexture->width, currentTexture->height, 1,
|
||||
this->format,
|
||||
this->type,
|
||||
currentTexture->data );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
texture2D * texture = this->textures->get( 0 );
|
||||
|
||||
glTexImage2D( this->target, 0, this->internalFormat, texture->width, texture->height, this->border, this->format, this->type, texture->data ); //GL_FLOAT
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( this->generateMipmap ) {
|
||||
|
||||
glGenerateMipmap( this->target );
|
||||
|
||||
}
|
||||
|
||||
|
||||
//glBindTexture( this->target, NULL );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
90
application/source/engine/shader.c
Normal file
90
application/source/engine/shader.c
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
|
||||
#include "block.h"
|
||||
|
||||
#include "member.h"
|
||||
|
||||
#include "uniform.h"
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../fileSystem.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "../char.h"
|
||||
|
||||
#include "sampler2D.h"
|
||||
|
||||
|
||||
class attribute{
|
||||
|
||||
GLchar name[64];
|
||||
|
||||
GLint location;
|
||||
|
||||
GLenum type;
|
||||
|
||||
}
|
||||
|
||||
class shader{
|
||||
|
||||
GLuint glShader;
|
||||
|
||||
constructor( GLuint shaderType ) {
|
||||
|
||||
this->glShader = glCreateShader( shaderType );
|
||||
|
||||
}
|
||||
|
||||
loadFromFile( char * shaderPath ) {
|
||||
|
||||
text * shaderSource = filesystem->readFile( shaderPath, "utf8" );
|
||||
|
||||
glShaderSource( this->glShader, 1, &shaderSource->value, NULL );
|
||||
|
||||
glCompileShader( this->glShader );
|
||||
|
||||
this->checkShaderForErrors( this->glShader );
|
||||
|
||||
}
|
||||
|
||||
checkShaderForErrors( GLuint shader ) {
|
||||
|
||||
GLint isCompiled = 0;
|
||||
|
||||
glGetShaderiv( shader, GL_COMPILE_STATUS, &isCompiled );
|
||||
|
||||
if( isCompiled == GL_FALSE )
|
||||
{
|
||||
GLint maxLength = 0;
|
||||
|
||||
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &maxLength );
|
||||
|
||||
GLchar errorMessage[ maxLength ];
|
||||
|
||||
glGetShaderInfoLog( shader, maxLength, &maxLength, errorMessage );
|
||||
|
||||
printf("\n\n\n\n Error: %s\n\n\n\n\n\n", errorMessage);
|
||||
|
||||
|
||||
glDeleteShader( shader );
|
||||
|
||||
exit( 0 );
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
26
application/source/engine/texture2D.c
Normal file
26
application/source/engine/texture2D.c
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include "vector2.h"
|
||||
|
||||
|
||||
class texture2D{
|
||||
|
||||
GLubyte * data;
|
||||
|
||||
int width;
|
||||
|
||||
int height;
|
||||
|
||||
int hasAlpha;
|
||||
|
||||
vector2 * offset;
|
||||
|
||||
}
|
||||
27
application/source/engine/uniform.c
Normal file
27
application/source/engine/uniform.c
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
|
||||
|
||||
class uniform{
|
||||
|
||||
char name[64];
|
||||
|
||||
GLint index;
|
||||
|
||||
GLint location;
|
||||
|
||||
GLint offset;
|
||||
|
||||
GLint size;
|
||||
|
||||
GLenum type;
|
||||
|
||||
}
|
||||
167
application/source/engine/uniformBlock.c
Normal file
167
application/source/engine/uniformBlock.c
Normal file
@@ -0,0 +1,167 @@
|
||||
|
||||
|
||||
#include "block.h"
|
||||
|
||||
#include "member.h"
|
||||
|
||||
#include <uniform.h>
|
||||
|
||||
|
||||
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fileSystem.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
#include "sampler2D.h"
|
||||
|
||||
|
||||
|
||||
class uniformBlock{
|
||||
|
||||
array * uniforms = new array();
|
||||
|
||||
GLint buffer;
|
||||
|
||||
GLuint index;
|
||||
|
||||
GLint size;
|
||||
|
||||
GLchar name[64];
|
||||
|
||||
GLuint bindingPoint;
|
||||
|
||||
float * data;
|
||||
|
||||
GLint autoUpload = 0;
|
||||
|
||||
createBuffer() {
|
||||
|
||||
unsigned int uniformBufferSize = this->size; // allocate 152 bytes of memory
|
||||
|
||||
printf("create GL_UNIFORM_BUFFER of size: %i index: %i \n", uniformBufferSize, this->index);
|
||||
|
||||
this->data = ( float * ) malloc( uniformBufferSize );
|
||||
|
||||
glGenBuffers( 1, &this->buffer );
|
||||
|
||||
glBindBuffer( GL_UNIFORM_BUFFER, this->buffer );
|
||||
|
||||
glBindBufferBase( GL_UNIFORM_BUFFER, this->index, this->buffer );
|
||||
|
||||
glBufferData( GL_UNIFORM_BUFFER, uniformBufferSize, 0, GL_DYNAMIC_DRAW );
|
||||
|
||||
//glBufferData( GL_UNIFORM_BUFFER, uniformBufferSize, NULL, GL_DYNAMIC_DRAW );
|
||||
|
||||
}
|
||||
|
||||
void upload() {
|
||||
|
||||
//printf("upload uniform buffer: %f %f %f %f\n", this->data[0], this->data[1], this->data[2], this->data[3]);
|
||||
|
||||
//printf("upload uniform buffer: %i\n", this->index);
|
||||
|
||||
glBindBuffer( GL_UNIFORM_BUFFER, this->buffer );
|
||||
|
||||
glBindBufferBase( GL_UNIFORM_BUFFER, this->bindingPoint, this->buffer );
|
||||
|
||||
glBufferData( GL_UNIFORM_BUFFER, this->size, this->data, GL_DYNAMIC_DRAW );
|
||||
|
||||
}
|
||||
|
||||
enableAutoUpload() {
|
||||
|
||||
this->autoUpload = 1;
|
||||
|
||||
}
|
||||
|
||||
void setUniform( char * name, void * value ) {
|
||||
|
||||
int uniformCount = this->uniforms->length();
|
||||
|
||||
//printf("uniformCount: %i\n\n", uniformCount);
|
||||
|
||||
for (int i = 0; i < uniformCount; ++i)
|
||||
{
|
||||
uniform * currentUniform = this->uniforms->get( i );
|
||||
|
||||
char * uniformName = (char *) currentUniform->name;
|
||||
|
||||
if( uniformName == name ) {
|
||||
|
||||
//printf("\n\n Update this uniform from uniform block %s\n\n", name);
|
||||
|
||||
switch( currentUniform->type ) {
|
||||
|
||||
case GL_FLOAT_VEC2:
|
||||
|
||||
vector2 * vector2Value = ( vector2 * ) value;
|
||||
|
||||
GLuint size = 8;
|
||||
|
||||
GLint offset = currentUniform->offset;
|
||||
|
||||
|
||||
if( this->autoUpload ) {
|
||||
|
||||
float data[2] = { vector2Value->x, vector2Value->y };
|
||||
|
||||
|
||||
glBindBuffer( GL_UNIFORM_BUFFER, this->buffer );
|
||||
|
||||
glBindBufferBase( GL_UNIFORM_BUFFER, 0, this->buffer );
|
||||
|
||||
// glBufferData( GL_UNIFORM_BUFFER, 16, data2, GL_DYNAMIC_DRAW );
|
||||
|
||||
glBufferSubData( GL_UNIFORM_BUFFER, offset, size, data );
|
||||
|
||||
//printf("using autoUpload %i %i %f %f\n", offset, size, data[0], data[1]);
|
||||
|
||||
} else {
|
||||
|
||||
GLint dataOffset = offset / sizeof( float );
|
||||
|
||||
this->data[ dataOffset ] = vector2Value->x;
|
||||
|
||||
this->data[ dataOffset + 1 ] = vector2Value->y;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GL_FLOAT_VEC3:
|
||||
|
||||
vector3 * vector3Value = ( vector3 * ) value;
|
||||
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case GL_SAMPLER_2D:
|
||||
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
156
application/source/engine/unsignedIntegerArray.c
Normal file
156
application/source/engine/unsignedIntegerArray.c
Normal file
@@ -0,0 +1,156 @@
|
||||
|
||||
#include <vector3.h>
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
|
||||
class unsignedIntegerArray{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
unsigned int * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
unsigned int get( int index )
|
||||
{
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
void set( int index, unsigned int item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
int * items = realloc( this->items, sizeof( int ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void addVector2( struct vector2 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
}
|
||||
|
||||
void addVector3( struct vector3 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
this->add( item->z );
|
||||
|
||||
}
|
||||
|
||||
void add( unsigned int item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this->items[index] = 0.0;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = 0.0;
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unsigned int array_push( unsigned int item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( int item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
unsigned int pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
int lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
213
application/source/engine/vector.c
Normal file
213
application/source/engine/vector.c
Normal file
@@ -0,0 +1,213 @@
|
||||
|
||||
|
||||
|
||||
#include "element.h"
|
||||
|
||||
#include "quadMesh.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../int.h"
|
||||
|
||||
|
||||
template<T>
|
||||
class vector{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
T * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
T get( int index )
|
||||
{
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
void set( int index, T item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
T * items = realloc( this->items, sizeof( T ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void add( T item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
|
||||
//this->items[index] = NULL;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
//this->items[i + 1] = NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// bug preprocessor + operator on pointer of vector2 or vector3.
|
||||
//memmove( this->items + index, this->items + index + 1, ( ( this->total - index ) - 1) * ( sizeof( T ) ) );
|
||||
|
||||
|
||||
this->total--;
|
||||
|
||||
//if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
// this->resize( this->capacity / 2 );
|
||||
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int array_push( T item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( T item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
/*
|
||||
bool includes() {
|
||||
|
||||
//#if TEMPLATE_1
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
T pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
T lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
template<>
|
||||
class <char *>vector{
|
||||
|
||||
includes( char * value ) {
|
||||
|
||||
int count = this->items->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
char * current = this->items->get( i );
|
||||
|
||||
if( current == value ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
class <int>vector{
|
||||
|
||||
bool includes( int value ) {
|
||||
|
||||
int count = this->items->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
int current = this->items->get( i );
|
||||
|
||||
if( current == value ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
56
application/source/engine/vector4.c
Normal file
56
application/source/engine/vector4.c
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
|
||||
|
||||
|
||||
class vector4{
|
||||
|
||||
float x;
|
||||
|
||||
float y;
|
||||
|
||||
float z;
|
||||
|
||||
float w;
|
||||
|
||||
|
||||
constructor( float x, float y, float z, float w ) {
|
||||
|
||||
this->x = x;
|
||||
|
||||
this->y = y;
|
||||
|
||||
this->z = z;
|
||||
|
||||
this->w = w;
|
||||
|
||||
}
|
||||
|
||||
vector4 * operator+( vector4 * b ) {
|
||||
|
||||
this->add( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
vector4 * operator+=( vector4 * b ) {
|
||||
|
||||
this->add( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
void add( vector4 * b ) {
|
||||
|
||||
this->x += b->x;
|
||||
|
||||
this->y += b->y;
|
||||
|
||||
this->z += b->z;
|
||||
|
||||
this->w += b->w;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
164
application/source/engine/windowManager.c
Normal file
164
application/source/engine/windowManager.c
Normal file
@@ -0,0 +1,164 @@
|
||||
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include <GL/gl.h> // GL 1.1 functions
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <hints.h>
|
||||
|
||||
static int DoubleBufferAttributes[] = {
|
||||
GLX_RGBA,
|
||||
GLX_RED_SIZE, 1,
|
||||
GLX_GREEN_SIZE, 1,
|
||||
GLX_BLUE_SIZE, 1,
|
||||
GLX_DEPTH_SIZE, 12,
|
||||
GLX_DOUBLEBUFFER,
|
||||
None,
|
||||
};
|
||||
|
||||
|
||||
|
||||
class windowManager{
|
||||
|
||||
Display * mainDisplay;
|
||||
|
||||
Window mainWindow;
|
||||
|
||||
int MainScreen;
|
||||
|
||||
Window RootWindow;
|
||||
|
||||
|
||||
setupDisplay() {
|
||||
|
||||
this->mainDisplay = XOpenDisplay( 0 );
|
||||
|
||||
this->MainScreen = XDefaultScreen( this->mainDisplay );
|
||||
|
||||
}
|
||||
|
||||
setupWindow() {
|
||||
|
||||
|
||||
this->RootWindow = XDefaultRootWindow( this->mainDisplay );
|
||||
|
||||
|
||||
int empty;
|
||||
|
||||
int ResultStatus = glXQueryExtension( this->mainDisplay, &empty, &empty );
|
||||
|
||||
XVisualInfo* VisualInfo = glXChooseVisual( this->mainDisplay, this->MainScreen, DoubleBufferAttributes );
|
||||
|
||||
GLXContext ShareList = None;
|
||||
|
||||
int IsDirectRendering = True;
|
||||
|
||||
|
||||
//XMatchVisualInfo( this->mainDisplay, this->MainScreen, 32, TrueColor, &VisualInfo);
|
||||
|
||||
|
||||
GLXContext OpenGLContext = glXCreateContext( this->mainDisplay, VisualInfo, ShareList, IsDirectRendering );
|
||||
|
||||
|
||||
|
||||
int WindowX = 0;
|
||||
|
||||
int WindowY = 0;
|
||||
|
||||
int WindowWidth = 1000;//;1920;
|
||||
|
||||
int WindowHeight = 1024;//;72;
|
||||
|
||||
int BorderWidth = 0;
|
||||
|
||||
int WindowClass = InputOutput;
|
||||
|
||||
int WindowDepth = VisualInfo->depth;
|
||||
|
||||
|
||||
Visual* WindowVisual = VisualInfo->visual;
|
||||
|
||||
int AttributeValueMask = CWBackPixel | CWEventMask | CWColormap;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
XSetWindowAttributes WindowAttributes = {};
|
||||
|
||||
WindowAttributes.colormap = XCreateColormap( this->mainDisplay, this->RootWindow, VisualInfo->visual, AllocNone );
|
||||
|
||||
//WindowAttributes.background_pixel = 0xffafe9af;
|
||||
|
||||
WindowAttributes.background_pixel = 0;
|
||||
|
||||
|
||||
WindowAttributes.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask;
|
||||
|
||||
this->mainWindow = XCreateWindow( this->mainDisplay, this->RootWindow,
|
||||
WindowX, WindowY, WindowWidth, WindowHeight,
|
||||
BorderWidth, WindowDepth, WindowClass, WindowVisual,
|
||||
AttributeValueMask, &WindowAttributes);
|
||||
|
||||
|
||||
//XMoveWindow( this->mainDisplay, this->mainWindow, 0, 800 );
|
||||
|
||||
XMoveWindow( this->mainDisplay, this->mainWindow, -400, 0 );
|
||||
|
||||
XStoreName( this->mainDisplay, this->mainWindow, "Opengl: Fixed function pipeline" );
|
||||
|
||||
glXMakeCurrent( this->mainDisplay, this->mainWindow, OpenGLContext );
|
||||
|
||||
XMapWindow( this->mainDisplay, this->mainWindow );
|
||||
|
||||
|
||||
//W = DisplayWidth(display, defaultScreen);
|
||||
//H = DisplayHeight(display, defaultScreen);
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Remove borders
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
Atom window_type = XInternAtom( this->mainDisplay, "_MOTIF_WM_HINTS", True );
|
||||
|
||||
Hints hints;
|
||||
|
||||
Atom property;
|
||||
|
||||
hints.flags = 2;
|
||||
|
||||
hints.decorations = 0;
|
||||
|
||||
XChangeProperty( this->mainDisplay, this->mainWindow, window_type, window_type, 32, PropModeReplace, ( unsigned char * ) & hints, 5 );
|
||||
|
||||
|
||||
*/
|
||||
|
||||
Atom WM_DELETE_WINDOW = XInternAtom( this->mainDisplay, "WM_DELETE_WINDOW", False );
|
||||
|
||||
|
||||
if( !XSetWMProtocols( this->mainDisplay, this->mainWindow, &WM_DELETE_WINDOW, 1) ) {
|
||||
|
||||
printf( "Couldn't register WM_DELETE_WINDOW\n" );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
75
application/source/examples/example.console.log.c
Normal file
75
application/source/examples/example.console.log.c
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#include "../console.h"
|
||||
|
||||
#include "../street.h"
|
||||
|
||||
#include "../user.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
struct user * newUser = new user();
|
||||
|
||||
newUser->id = 1;
|
||||
|
||||
newUser->username = "peter";
|
||||
|
||||
newUser->userlevel = 2134;
|
||||
|
||||
newUser->hash = "#234234325";
|
||||
|
||||
|
||||
|
||||
struct array * addresses = newUser->addresses;
|
||||
|
||||
|
||||
address * someAddress = new address();
|
||||
|
||||
someAddress->street = "HiLane";
|
||||
|
||||
someAddress->number = 1234;
|
||||
|
||||
addresses->add( someAddress );
|
||||
|
||||
|
||||
|
||||
|
||||
address * otherAddress = new address();
|
||||
|
||||
otherAddress->street = "OtherLane";
|
||||
|
||||
otherAddress->number = 4567;
|
||||
|
||||
addresses->add( otherAddress );
|
||||
|
||||
// todo newUser->addresses->add( otherAddress );
|
||||
|
||||
|
||||
printf("adresses count: %i\n\n", addresses->length() );
|
||||
|
||||
|
||||
char * something = "this is from an char * ";
|
||||
|
||||
int somethingElse = 123;
|
||||
|
||||
|
||||
console.log( "Goedendag",
|
||||
123456,
|
||||
"en een andere text.",
|
||||
something,
|
||||
somethingElse,
|
||||
"something en something",
|
||||
"in native c",
|
||||
23456,
|
||||
newUser,
|
||||
"and some text again" );
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
57
application/source/examples/example.file.system.c
Normal file
57
application/source/examples/example.file.system.c
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
|
||||
#include "../fileSystem.h"
|
||||
|
||||
#include "../console.h"
|
||||
|
||||
void main() {
|
||||
|
||||
char * directory2 = "./assets/";
|
||||
|
||||
fileSystem * filesystem = new fileSystem();
|
||||
|
||||
char * directory = "./assets/";
|
||||
|
||||
|
||||
|
||||
filesystem->writeFile( "./assets/testFile", "content of file" );
|
||||
|
||||
|
||||
array * files = filesystem->readDir( directory );
|
||||
|
||||
int fileCount = files->length();
|
||||
|
||||
for ( int i = 0; i < fileCount; ++i )
|
||||
{
|
||||
|
||||
char * filename = files->get( i );
|
||||
|
||||
char * path = directory->concatenate( filename );
|
||||
|
||||
char * source = filesystem->readFile( path );
|
||||
|
||||
|
||||
printf("filename: %s\n", path);
|
||||
|
||||
printf("source: %s\n\n\n", source);
|
||||
|
||||
}
|
||||
|
||||
if( filesystem->ensureDirectory("./Some/example/assure") ) {
|
||||
|
||||
console.log("Directories successfuly created.");
|
||||
|
||||
}
|
||||
|
||||
if( filesystem->exists( "Some" ) ) {
|
||||
|
||||
console.log("Directories 'Some' does exist.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
38
application/source/examples/example.multiple.inheritance.c
Normal file
38
application/source/examples/example.multiple.inheritance.c
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "extends.h"
|
||||
|
||||
void main() {
|
||||
|
||||
|
||||
|
||||
struct inherit * pointer = new inherit();
|
||||
|
||||
//pointer->propertyFromC = 20;
|
||||
|
||||
|
||||
printf("pointer->propertyFromC: %i\n\n", pointer->propertyFromC );
|
||||
|
||||
pointer->methodFromC();
|
||||
|
||||
printf("\n\n\n\n");
|
||||
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
|
||||
struct inherit instance = new inherit();
|
||||
|
||||
instance.propertyFromA = i;
|
||||
|
||||
instance.propertyFromB = i;
|
||||
|
||||
instance.propertyFromC = i;
|
||||
|
||||
instance.methodFromC();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
application/source/examples/example.opengl.c
Normal file
12
application/source/examples/example.opengl.c
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
#include "../engine/opengl.h"
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
opengl * instance = new opengl();
|
||||
|
||||
instance->initialize();
|
||||
|
||||
}
|
||||
166
application/source/examples/example.operator.overload.c
Normal file
166
application/source/examples/example.operator.overload.c
Normal file
@@ -0,0 +1,166 @@
|
||||
|
||||
|
||||
|
||||
|
||||
#include "../text.h"
|
||||
|
||||
#include "../vector2.h"
|
||||
|
||||
#include "../vector3.h"
|
||||
|
||||
#include "../console.h"
|
||||
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
text * textB = new text("ook mooi");
|
||||
|
||||
char * other = malloc(5000);
|
||||
|
||||
vector3 * newVector3 = new vector3();
|
||||
|
||||
newVector3->x = 2;
|
||||
|
||||
newVector3->y = 2;
|
||||
|
||||
newVector3->z = 3;
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
{
|
||||
|
||||
vector3 * vector3B = new vector3();
|
||||
|
||||
|
||||
vector3B->x = 2;
|
||||
|
||||
vector3B->y = 3;
|
||||
|
||||
vector3B->z = 4;
|
||||
|
||||
|
||||
vector3 * vector3C = new vector3();
|
||||
|
||||
|
||||
vector3C->x = 2;
|
||||
|
||||
vector3C->y = 3;
|
||||
|
||||
vector3C->z = 4;
|
||||
|
||||
newVector3 = vector3C + ( vector3C + newVector3 + ( ( ( ( vector3B + vector3C ) ) + vector3C ) + vector3B ) );
|
||||
|
||||
newVector3 = newVector3 + ( ( vector3B + vector3C ) + vector3C );
|
||||
|
||||
}
|
||||
|
||||
console->log( newVector3 );
|
||||
|
||||
text * textA = new text("mooi");
|
||||
|
||||
if( textA == textB ) {
|
||||
|
||||
printf("mooi zo");
|
||||
|
||||
}
|
||||
|
||||
text * textC = new text("mooi");
|
||||
|
||||
text * textD = new text("mooi");
|
||||
|
||||
|
||||
if( ( ( textA == textC ) && ( textA == textD ) ) && textA == textD ) {
|
||||
|
||||
printf("Good, This works.\n\n");
|
||||
|
||||
}
|
||||
|
||||
char * realChar = "something";
|
||||
|
||||
if( "something" == realChar ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( realChar == "something" ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( realChar == realChar ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
if( "something" == "something" && realChar == "something" ) {
|
||||
|
||||
printf("Wauw.\n\n");
|
||||
|
||||
}
|
||||
|
||||
char * fancy = "aaa ";
|
||||
|
||||
char * aaaa = " bbb ";
|
||||
|
||||
char * bbbb = " ccc ";
|
||||
|
||||
char * dddd = " dddd ";
|
||||
|
||||
other += ( fancy + ( bbbb + aaaa ) + dddd ) + " eee" + " het is een wonder ";
|
||||
|
||||
other += ("boven wonder" + returnText("this is a normal function") + " and this " + ( "works just good..." + dddd ));
|
||||
|
||||
other += "something" + ( returnText("this is a normal function") + textB->toNative() + textB->value );
|
||||
|
||||
other += ("and here some text") + (textB->toNative()) ;
|
||||
|
||||
other += ( textB->value + ( ( textB->value ) + returnText("this is a normal function") ) + (textB->toNative() + "here some text" + textB->value ) );
|
||||
|
||||
other += textB->value + textB->value + ( textB->value + textB->value ) + ( textB->value + textB->value ) + textB->value + textB->value;
|
||||
|
||||
console.log( "Mooi zo ", other );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
vector2 testVector = new vector2( 1, 450 );
|
||||
|
||||
|
||||
//int count = 0;
|
||||
|
||||
for (int i = 0; i < 2000000; ++i)
|
||||
{
|
||||
vector2 testVector1 = new vector2( 2, 450 );
|
||||
|
||||
vector2 * testVector2 = new vector2( 10, 450 );
|
||||
|
||||
testVector += testVector1 + testVector2;
|
||||
|
||||
//count++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
char * returnText( char * text ) {
|
||||
|
||||
return text;
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
154
application/source/examples/example.sqlite3.c
Normal file
154
application/source/examples/example.sqlite3.c
Normal file
@@ -0,0 +1,154 @@
|
||||
|
||||
|
||||
|
||||
|
||||
#include "../sqlite.h"
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "../console.h"
|
||||
|
||||
#include "../street.h"
|
||||
|
||||
#include "../user.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
void main() {
|
||||
|
||||
// sqlite * sql = new sqlite("./database/users.sqlite");
|
||||
|
||||
sqlite * sql = new sqlite(":memory:");
|
||||
|
||||
sql->selectModel( "user" );
|
||||
|
||||
|
||||
sql->createTable();
|
||||
|
||||
char * names[20] = { "piet", "gurbe", "siebe", "gerben", "twain", "piet", "gurbe", "siebe", "gerben", "twain", "piet", "gurbe", "siebe", "gerben", "twain" };
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
|
||||
array * usersInsertArray = new array();
|
||||
|
||||
for (int i = 0; i <10; ++i) //20000
|
||||
{
|
||||
|
||||
char * name = names[i];
|
||||
|
||||
struct user * userInstance = new user();
|
||||
|
||||
userInstance->id = i;
|
||||
|
||||
userInstance->username = name;
|
||||
|
||||
userInstance->userlevel = 3;
|
||||
|
||||
userInstance->hash = "12345";
|
||||
|
||||
usersInsertArray.add( userInstance );
|
||||
|
||||
//sql->addRow( userInstance );
|
||||
|
||||
}
|
||||
|
||||
|
||||
sql->addRows( usersInsertArray );
|
||||
|
||||
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("Multiply: %f seconds, rows: %i\n", (double)( toc - tic ) / CLOCKS_PER_SEC, 10000);
|
||||
|
||||
|
||||
|
||||
array * usersA = sql->fetchRows( "SELECT * FROM user" );
|
||||
|
||||
//user * firstUser = new user();
|
||||
|
||||
user * firstUser = usersA->get( 1 );
|
||||
|
||||
//firstUser->id =10;
|
||||
|
||||
firstUser->username = "updated";
|
||||
|
||||
firstUser->userlevel = 1111;
|
||||
|
||||
firstUser->hash = "new hash";
|
||||
|
||||
console->log( "??", firstUser, "Yeey console.log workswith , , " );
|
||||
|
||||
|
||||
sql->update( firstUser );
|
||||
|
||||
printf("before log\n\n");
|
||||
|
||||
|
||||
|
||||
|
||||
array * users = sql->fetchRows( "SELECT * FROM user " );
|
||||
|
||||
int userCount = users->total;
|
||||
|
||||
|
||||
printf("added rows: %i\n", userCount);
|
||||
|
||||
console->createHorisontalLine();
|
||||
|
||||
printf("\e[1m");
|
||||
|
||||
printf(" ");
|
||||
|
||||
printf("%-20s", "id" );
|
||||
|
||||
printf("%-30s", "username" );
|
||||
|
||||
printf("%-20s", "userLevel" );
|
||||
|
||||
printf("%-30s", "Hash");
|
||||
|
||||
printf("\e[m");
|
||||
|
||||
printf("\n");
|
||||
|
||||
console->createHorisontalLine();
|
||||
|
||||
printf("users: %i\n", userCount);
|
||||
|
||||
for (int i = 0; i < userCount; ++i)
|
||||
{
|
||||
|
||||
struct user * userInstance = users->get( i );
|
||||
|
||||
userInstance->id;
|
||||
|
||||
userInstance->username;
|
||||
|
||||
printf(" ");
|
||||
|
||||
printf("%-20i", userInstance->id);
|
||||
|
||||
printf("%-30s", userInstance->username);
|
||||
|
||||
printf("%-20i", userInstance->userlevel);
|
||||
|
||||
printf("%-30s", userInstance->hash);
|
||||
|
||||
printf( "\n" );//%-30s
|
||||
|
||||
}
|
||||
|
||||
sql->free();
|
||||
|
||||
console->createHorisontalLine();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
78
application/source/examples/example.text.c
Executable file
78
application/source/examples/example.text.c
Executable file
@@ -0,0 +1,78 @@
|
||||
|
||||
|
||||
#include "../array.h"
|
||||
|
||||
#include "../text.h"
|
||||
|
||||
#include "time.h"
|
||||
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
text * sometext = new text(" my first text");
|
||||
|
||||
struct text a = new text(" Mooi ");
|
||||
|
||||
for ( int i = 0; i < 100; ++i ) {
|
||||
|
||||
sometext->appendObject( a )->append("something")->appendObject( new text(" and this") );
|
||||
|
||||
}
|
||||
|
||||
printf("append: %s \n\n", sometext->buffer);
|
||||
|
||||
clock_t toc = clock();
|
||||
|
||||
printf("Multiply: %f seconds\n", (double)( toc - tic ) / CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
char * testText = "a,";
|
||||
|
||||
array * splitParts = testText->concatenate( "b, " )->concatenate("c,d,e")->concatenate(",f ")->split(",g");
|
||||
|
||||
char * compareString = "something";
|
||||
|
||||
splitParts.unshift((char *) "first");
|
||||
|
||||
splitParts.unshift((char *) "this first");
|
||||
|
||||
|
||||
printf( "joined: %s\n\n", splitParts.join(", ") );
|
||||
|
||||
char * lastElement = splitParts->pop();
|
||||
|
||||
printf( "pop joined: %s\n\n", splitParts.join(", ") );
|
||||
|
||||
printf( "pop removed element: %s\n\n", lastElement );
|
||||
|
||||
|
||||
if( compareString.compare("something") ) {
|
||||
|
||||
printf("this is true\n");
|
||||
|
||||
} else {
|
||||
|
||||
printf("this is not true\n");
|
||||
|
||||
}
|
||||
|
||||
printf("first: %s \n\n", splitParts.get( 0 ) );
|
||||
|
||||
int splitCount = splitParts->length();
|
||||
|
||||
printf("splitCount: %i\n", splitCount);
|
||||
|
||||
for (int i = 0; i < splitCount; ++i)
|
||||
{
|
||||
|
||||
char * splitPart = splitParts.get( i );
|
||||
|
||||
//printf("splitPart: %s \n\n", splitPart->trim() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
138
application/source/examples/example.vector.c
Normal file
138
application/source/examples/example.vector.c
Normal file
@@ -0,0 +1,138 @@
|
||||
|
||||
|
||||
|
||||
#include "vector2.h"
|
||||
|
||||
#include "vector3.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "triangle.h"
|
||||
|
||||
void main() {
|
||||
|
||||
array * arrayA = new array();
|
||||
|
||||
clock_t tic = clock();
|
||||
|
||||
vector2 * testVector = new vector2( 1, 450 );
|
||||
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < 1000000; ++i)
|
||||
{
|
||||
vector2 * testVector1 = new vector2( i, i * 450 );
|
||||
|
||||
vector2 * testVector2 = new vector2( 10, 450 );
|
||||
|
||||
testVector += testVector1 + testVector2;
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("count: %i\n", count);
|
||||
|
||||
|
||||
for ( int i = 0; i < 1; ++i )
|
||||
{
|
||||
|
||||
vector2 testVector = new vector2( 10, 450 );
|
||||
|
||||
|
||||
arrayA->add( &testVector );
|
||||
|
||||
arrayA->add( &testVector );
|
||||
|
||||
|
||||
//printf("number of items in array: %i \n ", arrayA->length() );
|
||||
|
||||
vector2 * returned = arrayA->get( i );
|
||||
|
||||
//printf("returned vector x: %i \n ", returned->x );
|
||||
|
||||
//printf("returned vector y: %i \n ", returned->y );
|
||||
|
||||
vector2 testVector3 = new vector2( 2, 3 );
|
||||
|
||||
arrayA->set( i, &testVector3 );
|
||||
|
||||
vector2 * secondReturn = arrayA->get( 0 );
|
||||
|
||||
//printf("returned vector x: %i \n ", secondReturn->x );
|
||||
|
||||
//printf("returned vector y: %i \n ", secondReturn->y );
|
||||
|
||||
returned->add( secondReturn );
|
||||
|
||||
printf( "returned vector x: %i \n ", returned->x );
|
||||
|
||||
printf("returned vector y: %i \n ", returned->y );
|
||||
|
||||
}
|
||||
|
||||
|
||||
for ( int i = 0; i < 1; ++i )
|
||||
{
|
||||
|
||||
vector2 a = new vector2( 10, 10 );
|
||||
|
||||
vector2 b = new vector2( 10, 10 );
|
||||
|
||||
a->add( &b );
|
||||
|
||||
printf( "a->x: %i, a->y: %i\n", a.x, a.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
triangle * a = new triangle();
|
||||
|
||||
triangle * b = new triangle();
|
||||
|
||||
|
||||
a->add( b );
|
||||
}
|
||||
|
||||
|
||||
vector2 * t = new vector2( 2,2 );
|
||||
|
||||
int totalLength = 0;
|
||||
|
||||
for (int i = 0; i < 1; ++i)
|
||||
{
|
||||
|
||||
t->x = 100;
|
||||
|
||||
vector2 b = new vector2( 123, 2 );
|
||||
|
||||
b.x = 50;
|
||||
|
||||
vector2 c = new vector2( 1, 12 );
|
||||
|
||||
|
||||
c.x = i;
|
||||
|
||||
t->add( &b );
|
||||
|
||||
b.add( &c );
|
||||
|
||||
c.add( &b );
|
||||
|
||||
printf("a, %i\n", t->x);
|
||||
|
||||
printf("b, %i\n", b.x);
|
||||
|
||||
printf("c, %i\n", c.x);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
18
application/source/examples/example.vulkan.c
Normal file
18
application/source/examples/example.vulkan.c
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
#include "vulkan.h"
|
||||
volatile sig_atomic_t stop;
|
||||
|
||||
void inthand(int signum) {
|
||||
stop = 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vulkan * instance = new vulkan();
|
||||
|
||||
instance->setup();
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
70
application/source/examples/example.web.server.c
Normal file
70
application/source/examples/example.web.server.c
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
#include "../http.h"
|
||||
|
||||
#include "./fileSystem.h"
|
||||
|
||||
#include "./text.h"
|
||||
|
||||
#include "./mimeTypes.h"
|
||||
|
||||
#include "./cache.h"
|
||||
|
||||
|
||||
struct cache * cacheManager;
|
||||
|
||||
|
||||
|
||||
void handleRequest( struct request * requestInstance, struct text * response ) {
|
||||
|
||||
text * filePath = new text("www/");
|
||||
|
||||
filePath += requestInstance->url;
|
||||
|
||||
|
||||
if( filesystem->isDirectory( filePath->value ) ) {
|
||||
|
||||
filePath->value += "index.html";
|
||||
|
||||
requestInstance->mimeType = "text/html";
|
||||
|
||||
}
|
||||
|
||||
text * content = cacheManager->getFile( filePath->value );
|
||||
|
||||
//text * content = filesystem->readFile( filePath->value, "binary" );
|
||||
|
||||
response += "HTTP/1.0 200 OK\r\n";
|
||||
|
||||
response += "Server: webserver-c\r\n";
|
||||
|
||||
response += "Content-type: " + requestInstance->mimeType + "\r\n\r\n";
|
||||
|
||||
printf("Filename: %s\n", filePath->value);
|
||||
|
||||
printf("Source: %s\n", content->value);
|
||||
|
||||
response->appendObject( content );
|
||||
|
||||
response += "\r\n";
|
||||
|
||||
|
||||
|
||||
filePath->free();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
|
||||
cacheManager = new cache();
|
||||
|
||||
|
||||
|
||||
http * serverInstance = new http();
|
||||
|
||||
serverInstance->createServer( handleRequest );
|
||||
|
||||
serverInstance->listen( 8080 );
|
||||
|
||||
}
|
||||
87
application/source/extends.c
Normal file
87
application/source/extends.c
Normal file
@@ -0,0 +1,87 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class classB{
|
||||
|
||||
int propertyFromC = 111;
|
||||
|
||||
void methodFromC() {
|
||||
|
||||
printf("Please print much much..");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class classA{
|
||||
|
||||
int propertyFromC = 100;
|
||||
|
||||
void methodFromC() {
|
||||
|
||||
printf("Don't print soo much...");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class inherit extends a, classB, classA{
|
||||
|
||||
int propertyFromC = 20;
|
||||
|
||||
void methodFromC() {
|
||||
|
||||
printf("this is a method from c\n\n");
|
||||
|
||||
|
||||
|
||||
printf("from c: a : %i\n", this->propertyFromA);
|
||||
printf("from c: b : %i\n", this->propertyFromB);
|
||||
printf("from c: c : %i\n", this->propertyFromC);
|
||||
|
||||
this->propertyFromB++;
|
||||
|
||||
this->methodFromA();
|
||||
|
||||
this->propertyFromB++;
|
||||
|
||||
this->methodFromB();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class a extends b{
|
||||
|
||||
int propertyFromA = 10;
|
||||
|
||||
void methodFromA() {
|
||||
|
||||
printf("this is a method from a\n\n");
|
||||
|
||||
printf("from a: a : %i\n", this->propertyFromA);
|
||||
printf("from a: b : %i\n", this->propertyFromB);
|
||||
printf("from a: c : %i\n", this->propertyFromC);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class b{
|
||||
|
||||
int propertyFromB = 1234;
|
||||
|
||||
void methodFromB() {
|
||||
|
||||
printf("this is a method from b\n\n");
|
||||
|
||||
printf("from b: a : %i\n", this->propertyFromA);
|
||||
printf("from b: b : %i\n", this->propertyFromB);
|
||||
printf("from b: c : %i\n", this->propertyFromC);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
10
application/source/file.c
Normal file
10
application/source/file.c
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
#include "text.h"
|
||||
|
||||
class file{
|
||||
|
||||
char * filePath;
|
||||
|
||||
text * content;
|
||||
|
||||
}
|
||||
264
application/source/fileSystem.c
Executable file
264
application/source/fileSystem.c
Executable file
@@ -0,0 +1,264 @@
|
||||
|
||||
|
||||
#include <http.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <array.h>
|
||||
|
||||
#include <console.h>
|
||||
|
||||
|
||||
|
||||
#include <char.h>
|
||||
|
||||
#include <dirent.h> // "DIR *"
|
||||
|
||||
#include <unistd.h> // access
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
extern struct fileSystem * filesystem;
|
||||
|
||||
class fileSystem{
|
||||
|
||||
void writeFile( char * filepath, char * data )
|
||||
{
|
||||
FILE *fp = fopen( filepath, "wb" );
|
||||
|
||||
if (fp != NULL)
|
||||
{
|
||||
fputs( data, fp );
|
||||
|
||||
fclose( fp );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct array * readDir( char * filePath ) {
|
||||
|
||||
DIR * dir;
|
||||
|
||||
struct dirent * entry;
|
||||
|
||||
struct array * files = new array();
|
||||
|
||||
if ( ( dir = opendir( filePath ) ) == NULL ){
|
||||
|
||||
perror("opendir() error");
|
||||
|
||||
} else {
|
||||
|
||||
while( ( entry = readdir( dir ) ) != NULL ) {
|
||||
|
||||
char * filename = (char *)entry->d_name;
|
||||
|
||||
if ( strcmp( filename, ".." ) != 0 && strcmp( filename, "." ) != 0 ) {
|
||||
|
||||
array_add( files, filename );
|
||||
|
||||
//printf("%s\n", filename);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
//closedir(dir);
|
||||
}
|
||||
|
||||
|
||||
struct text * readFile( char * name, char * mode ) {
|
||||
|
||||
char * readMode;
|
||||
|
||||
if( mode == "utf8" ) {
|
||||
|
||||
readMode = "r";
|
||||
|
||||
} else {
|
||||
|
||||
readMode = "rb";
|
||||
|
||||
}
|
||||
|
||||
FILE * file = fopen( name, readMode );
|
||||
|
||||
if ( file == NULL ) {
|
||||
|
||||
fprintf( stderr, "Error: Can't open file '%s'.", name );
|
||||
|
||||
exit( EXIT_FAILURE );
|
||||
|
||||
}
|
||||
|
||||
fseek( file, 0, SEEK_END );
|
||||
|
||||
long length = ftell( file );
|
||||
|
||||
//printf("buffer length is '%i' \n\n", length);
|
||||
|
||||
fseek( file, 0, SEEK_SET );
|
||||
|
||||
char * buffer = malloc( sizeof( char ) * ( length + 1 ) );
|
||||
|
||||
fread( buffer, sizeof( char ), length, file );
|
||||
|
||||
fclose( file );
|
||||
|
||||
if( mode == "utf8" ) {
|
||||
|
||||
buffer[ length ] = 0;
|
||||
|
||||
}
|
||||
|
||||
text * output = new text( "" );
|
||||
|
||||
output->length = 0;
|
||||
|
||||
output->appendBinary( buffer, length );
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
char * readBinaryFile( char * name, char * mode, int * size ) {
|
||||
|
||||
char * readMode;
|
||||
|
||||
if( mode == "utf8" ) {
|
||||
|
||||
readMode = "r";
|
||||
|
||||
} else {
|
||||
|
||||
readMode = "rb";
|
||||
|
||||
printf("readmode = rb binary\n\n");
|
||||
|
||||
}
|
||||
|
||||
FILE * file = fopen( name, readMode );
|
||||
|
||||
if ( file == NULL ) {
|
||||
|
||||
fprintf( stderr, "Error: Can't open file '%s'.", name );
|
||||
|
||||
exit( EXIT_FAILURE );
|
||||
|
||||
}
|
||||
|
||||
fseek( file, 0, SEEK_END );
|
||||
|
||||
long length = ftell( file );
|
||||
|
||||
printf("buffer length is '%i' \n\n", length);
|
||||
|
||||
fseek( file, 0, SEEK_SET );
|
||||
|
||||
char * buffer = malloc( sizeof( char ) * ( length + 1 ) );
|
||||
|
||||
buffer[ length ] = '\0';
|
||||
|
||||
fread( buffer, sizeof( char ), length, file );
|
||||
|
||||
fclose( file );
|
||||
|
||||
|
||||
//buffer[ length ] = 0;
|
||||
|
||||
*size = length;
|
||||
|
||||
printf("strlen: %i \n\n", length);
|
||||
|
||||
return buffer;
|
||||
|
||||
}
|
||||
|
||||
int ensureDirectory( char * path ) {
|
||||
|
||||
char * pathCopy = path->copy();
|
||||
|
||||
struct array * parts = pathCopy->split( "/" );
|
||||
|
||||
int count = parts->length( );
|
||||
|
||||
for ( int i = 1; i < count; ++i )
|
||||
{
|
||||
struct array * tempParts = pathCopy->split( "/" );
|
||||
|
||||
for ( int j = 0; j < count-i-1; ++j )
|
||||
{
|
||||
tempParts->pop();
|
||||
}
|
||||
|
||||
char * tempPath = tempParts->join( "/" );
|
||||
|
||||
if( this->exists( tempPath ) ) {
|
||||
|
||||
} else {
|
||||
|
||||
this->makeDirectory( tempPath );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int makeDirectory( char * path ) {
|
||||
|
||||
if ( this->exists( path ) == NULL ) {
|
||||
|
||||
mkdir( path, 0700 );
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int exists( char * path ) {
|
||||
|
||||
if ( access( path, F_OK ) == 0 ) {
|
||||
|
||||
return 1;
|
||||
|
||||
} else {
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int isDirectory( char * path ) {
|
||||
|
||||
struct stat path_stat;
|
||||
|
||||
stat( path, & path_stat );
|
||||
|
||||
if( S_ISREG( path_stat.st_mode ) ) {
|
||||
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
8
application/source/header.c
Normal file
8
application/source/header.c
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
class header{
|
||||
|
||||
char * name;
|
||||
|
||||
char * value;
|
||||
|
||||
}
|
||||
167
application/source/headers.c
Normal file
167
application/source/headers.c
Normal file
@@ -0,0 +1,167 @@
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "header.h"
|
||||
|
||||
class headerManager{
|
||||
|
||||
struct array * headers = new array();
|
||||
|
||||
parse( char * headerContent ) {
|
||||
|
||||
array * headerRows = headerContent->split("\n");
|
||||
|
||||
int headerCount = headerRows->length();
|
||||
|
||||
for (int i = 1; i < headerCount; ++i)
|
||||
{
|
||||
|
||||
char * headerRow = headerRows->get( i );
|
||||
|
||||
array * headerRowParts = headerRow->split(":");
|
||||
|
||||
int headerRowPartsCount = headerRowParts->length();
|
||||
|
||||
|
||||
if( headerRowPartsCount == 2 ) {
|
||||
|
||||
char * headerName = headerRowParts->get( 0 );
|
||||
|
||||
char * headerValue = headerRowParts->get( 1 );
|
||||
|
||||
//printf("%-20s %-30s \n", headerName, headerValue->removeWhitespace());
|
||||
|
||||
//printf("header value: %s\n\n", headerValue->removeWhitespace() );
|
||||
|
||||
this->add( headerName, headerValue->removeWhitespace() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
}
|
||||
|
||||
void display() {
|
||||
|
||||
struct array * headerRows = this->headers;
|
||||
|
||||
int headerCount = headerRows->length();
|
||||
|
||||
for (int i = 0; i < headerCount; ++i)
|
||||
{
|
||||
struct header * headerInstance = headerRows->get( i );
|
||||
|
||||
printf("%-20s %-30s \n", headerInstance->name, headerInstance->value);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void add( char * name, char * value ) {
|
||||
|
||||
header * headerInstance = new header();
|
||||
|
||||
headerInstance->name = name;
|
||||
|
||||
headerInstance->value = value;
|
||||
|
||||
this->headers->add( headerInstance );
|
||||
|
||||
}
|
||||
|
||||
void set( char * name, char * value ) {
|
||||
|
||||
struct header * headerInstance = this->get( name );
|
||||
|
||||
if( headerInstance == NULL ) {
|
||||
|
||||
this->add( name, value );
|
||||
|
||||
} else {
|
||||
|
||||
int headerIndex = this->getHeaderIndex( name );
|
||||
|
||||
array * headers = this->headers;
|
||||
|
||||
|
||||
header * headerInstance = headers->get( headerIndex );
|
||||
|
||||
headerInstance->value = value;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int getHeaderIndex( char * name ) {
|
||||
|
||||
array * headers = this->headers;
|
||||
|
||||
int count = headers->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
header * headerInstance = headers->get( i );
|
||||
|
||||
if( headerInstance->name == name ) {
|
||||
|
||||
return i;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
char * getValue( char * name ) {
|
||||
|
||||
array * headers = this->headers;
|
||||
|
||||
int count = headers->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
header * headerInstance = headers->get( i );
|
||||
|
||||
if( headerInstance->name == name ) {
|
||||
|
||||
return headerInstance->value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
header * get( char * name ) {
|
||||
|
||||
array * headers = this->headers;
|
||||
|
||||
int count = headers->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
header * headerInstance = headers->get( i );
|
||||
|
||||
if( headerInstance->name == name ) {
|
||||
|
||||
return headerInstance;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
507
application/source/http.c
Normal file
507
application/source/http.c
Normal file
@@ -0,0 +1,507 @@
|
||||
// webserver.c
|
||||
|
||||
|
||||
#include "./fileSystem.h"
|
||||
|
||||
#include "./text.h"
|
||||
|
||||
#include "./mimeTypes.h"
|
||||
|
||||
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <cache.h>
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <request.h>
|
||||
|
||||
#include <headers.h>
|
||||
|
||||
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
|
||||
|
||||
class http{
|
||||
|
||||
int socket;
|
||||
|
||||
struct sockaddr_in * hostAddress = malloc( sizeof( struct sockaddr_in ) );
|
||||
|
||||
int hostAddresslength = sizeof( struct sockaddr_in );
|
||||
|
||||
struct fileSystem * filesystem = new fileSystem();
|
||||
|
||||
struct mimeTypes * mimetypes = new mimeTypes();
|
||||
|
||||
struct headerManager * headers = new headerManager();
|
||||
|
||||
int useSSL = -1;
|
||||
|
||||
|
||||
SSL_CTX * sslContext;
|
||||
|
||||
|
||||
|
||||
void ( * requestCallback )( struct request * requestInstance, struct text * response );
|
||||
|
||||
|
||||
void createServer( void ( * requestCallback )( request * req, text * response ) ) {
|
||||
|
||||
this->requestCallback = requestCallback;
|
||||
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
this->initializeOpenSSL();
|
||||
|
||||
}
|
||||
|
||||
printf("after initializeOpenSSL\n\n");
|
||||
|
||||
}
|
||||
|
||||
initializeOpenSSL() {
|
||||
|
||||
// OpenSSL 1.1.0 or above initializes by itself
|
||||
//SSL_load_error_strings();
|
||||
//ssl_load_ciphers();
|
||||
//OpenSSL_add_all_algorithms();
|
||||
|
||||
/* Lets get nice error messages */
|
||||
SSL_load_error_strings();
|
||||
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
SSLeay_add_ssl_algorithms();
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
int listen( int port ) {
|
||||
|
||||
this->socket = socket( AF_INET, SOCK_STREAM, 0 );
|
||||
|
||||
int iSetOption = 1;
|
||||
|
||||
setsockopt( this->socket, SOL_SOCKET, SO_REUSEADDR, ( char * ) & iSetOption, sizeof( iSetOption ) );
|
||||
|
||||
if ( this->socket == -1 ) {
|
||||
|
||||
perror("webserver (socket)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("socket created successfully\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Create the address to bind the socket to
|
||||
this->hostAddress->sin_family = AF_INET;
|
||||
|
||||
this->hostAddress->sin_port = htons( port );
|
||||
|
||||
this->hostAddress->sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
|
||||
|
||||
// Bind the socket to the address
|
||||
if ( bind( this->socket, ( struct sockaddr * ) this->hostAddress, this->hostAddresslength ) != 0 ) {
|
||||
|
||||
perror("webserver (bind)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("socket successfully bound to address\n");
|
||||
|
||||
}
|
||||
|
||||
// Listen for incoming connections
|
||||
if ( listen( this->socket, SOMAXCONN ) != 0 ) {
|
||||
|
||||
perror("webserver (listen)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("server listening for connections\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (;;) {
|
||||
|
||||
this->acceptConnection( );
|
||||
|
||||
}
|
||||
|
||||
|
||||
//this->DestroySSL();
|
||||
|
||||
printf("exit");
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
char * getExtension( char * path ) {
|
||||
|
||||
array * parts = path->split(".");
|
||||
|
||||
int count = parts->length();
|
||||
|
||||
return parts->get( count - 1 );
|
||||
|
||||
}
|
||||
|
||||
logRequest( request * requestInstance ) {
|
||||
|
||||
printf("server address: [%s:%u]\n", requestInstance->address, requestInstance->port );
|
||||
|
||||
printf("mimeType: %-30s \n", requestInstance->mimeType);
|
||||
|
||||
printf("header: %-30s \n", requestInstance->extension );
|
||||
|
||||
printf("method: %-30s \n", requestInstance->method );
|
||||
|
||||
printf("version: %-30s \n\n", requestInstance->version );
|
||||
|
||||
}
|
||||
|
||||
acceptConnection( ) {
|
||||
|
||||
fileSystem * filesystem = this->filesystem;
|
||||
|
||||
mimeTypes * mimetypes = this->mimetypes;
|
||||
|
||||
text * response = new text("");
|
||||
|
||||
|
||||
|
||||
char buffer[ 4096 ];
|
||||
|
||||
// Create client address
|
||||
struct sockaddr_in client_addr;
|
||||
|
||||
int client_addrlen = sizeof(client_addr);
|
||||
|
||||
// Accept incoming connections
|
||||
int socketConnection = accept( this->socket,
|
||||
(struct sockaddr *)this->hostAddress,
|
||||
( socklen_t * ) & this->hostAddresslength );
|
||||
|
||||
|
||||
/*
|
||||
|
||||
struct rusage r_usage;
|
||||
|
||||
getrusage(RUSAGE_SELF,&r_usage);
|
||||
// Print the maximum resident set size used (in kilobytes).
|
||||
|
||||
printf("Memory usage: %ld kilobytes\n\n",r_usage.ru_maxrss);
|
||||
|
||||
free( r_usage );
|
||||
*/
|
||||
|
||||
SSL * ssl = NULL;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
const SSL_METHOD * method = SSLv23_server_method();
|
||||
|
||||
this->sslContext = SSL_CTX_new( method );
|
||||
|
||||
if ( this->sslContext == NULL ) {
|
||||
|
||||
printf("Error loading SSL_CTX_NEW '%s'\n\n", stderr);
|
||||
|
||||
}
|
||||
|
||||
SSL_CTX_set_options( this->sslContext, SSL_OP_SINGLE_DH_USE );
|
||||
|
||||
int use_cert = SSL_CTX_use_certificate_file( this->sslContext, "/etc/letsencrypt/live/unifyjs.org/fullchain.pem" , SSL_FILETYPE_PEM );
|
||||
|
||||
int use_prv = SSL_CTX_use_PrivateKey_file( this->sslContext, "/etc/letsencrypt/live/unifyjs.org/privkey.pem", SSL_FILETYPE_PEM );
|
||||
|
||||
if( use_cert != 1 ) {
|
||||
|
||||
printf( "error: SSL_CTX_use_certificate_file\n\n" );
|
||||
|
||||
}
|
||||
|
||||
if( use_prv != 1 ) {
|
||||
|
||||
printf( "error: SSL_CTX_use_PrivateKey_file\n\n" );
|
||||
|
||||
}
|
||||
|
||||
if ( !SSL_CTX_check_private_key(this->sslContext) ) {
|
||||
|
||||
printf("Private key does not match the certificate public key\n");
|
||||
|
||||
// initiate page describing error.
|
||||
}
|
||||
|
||||
ssl = SSL_new( this->sslContext );
|
||||
|
||||
SSL_set_fd( ssl, socketConnection );
|
||||
|
||||
//Here is the SSL Accept portion. Now all reads and writes must use SSL
|
||||
int ssl_err = SSL_accept( ssl ); // SSL routines:SSL_UNDEFINED_FUNCTION:called a function you should not call
|
||||
|
||||
//printf ("SSL connection using %s\n", SSL_get_cipher( ssl ) );
|
||||
|
||||
//ERR_print_errors_fp( stderr );
|
||||
|
||||
if( ssl_err == 0 ){
|
||||
|
||||
printf("SSL_accept returned zero\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
int n;
|
||||
|
||||
if( ssl_err < 0 ) {
|
||||
|
||||
int err;
|
||||
|
||||
if( ( err = SSL_get_error(ssl,n ) ) == SSL_ERROR_WANT_READ) {
|
||||
|
||||
printf("SSL_accept wants more data\n");
|
||||
|
||||
return ;
|
||||
|
||||
}
|
||||
|
||||
exit(7);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
printf ("SSL connection using %s\n", SSL_get_cipher (this->sslContext));
|
||||
|
||||
#define CHK_NULL(x) if ((x)==NULL) exit (1)
|
||||
|
||||
X509* client_cert;
|
||||
|
||||
char* str;
|
||||
|
||||
// Get client's certificate (note: beware of dynamic allocation) - opt
|
||||
|
||||
client_cert = SSL_get_peer_certificate (this->sslContext);
|
||||
|
||||
if ( client_cert != NULL ) {
|
||||
|
||||
printf ("Client certificate:\n");
|
||||
|
||||
str = X509_NAME_oneline (X509_get_subject_name (client_cert), 0, 0);
|
||||
|
||||
CHK_NULL(str);
|
||||
|
||||
printf ("\t subject: %s\n", str);
|
||||
|
||||
OPENSSL_free (str);
|
||||
|
||||
str = X509_NAME_oneline (X509_get_issuer_name (client_cert), 0, 0);
|
||||
|
||||
CHK_NULL(str);
|
||||
|
||||
printf ("\t issuer: %s\n", str);
|
||||
|
||||
OPENSSL_free (str);
|
||||
|
||||
X509_free (client_cert);
|
||||
|
||||
} else {
|
||||
|
||||
printf ("Client does not have certificate.\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ( socketConnection == -1 ) {
|
||||
|
||||
perror("webserver (accept)");
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
//printf("connection accepted2\n\n");
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
printf("connection accepted\n\n");
|
||||
|
||||
// Get client address
|
||||
int sockn = getsockname( socketConnection,
|
||||
( struct sockaddr * ) &client_addr,
|
||||
( socklen_t * ) &client_addrlen);
|
||||
|
||||
|
||||
if ( sockn == -1 ) {
|
||||
|
||||
perror("webserver (getsockname)");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int valread;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
valread = SSL_read( ssl, buffer, 4096 - 1 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("read without ssl\n\n");
|
||||
|
||||
int valread = read( socketConnection, buffer, 4096 -1 );
|
||||
|
||||
if ( valread == -1 ) {
|
||||
|
||||
perror("webserver (read)");
|
||||
|
||||
//return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( valread == -1 ) {
|
||||
|
||||
perror("webserver (read)");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
request * requestInstance = new request();
|
||||
|
||||
|
||||
sscanf( buffer, "%s %s %s", requestInstance->method, requestInstance->url, requestInstance->version );
|
||||
|
||||
|
||||
requestInstance->address = inet_ntoa( client_addr.sin_addr );
|
||||
|
||||
requestInstance->port = ntohs( client_addr.sin_port );
|
||||
|
||||
requestInstance->extension = this->getExtension( requestInstance->url );
|
||||
|
||||
requestInstance->mimeType = mimetypes->getByExtension( requestInstance->extension );
|
||||
|
||||
this->requestCallback( requestInstance, response );
|
||||
|
||||
|
||||
|
||||
|
||||
//printf("response: %s",response->value);
|
||||
|
||||
int writeResponse;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
writeResponse = SSL_write( ssl, response->value, response->length );
|
||||
|
||||
} else {
|
||||
|
||||
// Write to the socket
|
||||
int writeResponse = write( socketConnection, response->value, response->length );
|
||||
|
||||
printf("close connection");
|
||||
|
||||
if ( writeResponse == -1 ) {
|
||||
|
||||
perror("webserver (write)");
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( writeResponse == -1 ) {
|
||||
|
||||
perror("webserver (write)");
|
||||
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
//printf("written a response\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
//free( buffer );
|
||||
|
||||
//SSL_free( writeResponse );
|
||||
|
||||
//response->free();
|
||||
|
||||
//requestInstance->free();
|
||||
|
||||
close( socketConnection );
|
||||
|
||||
//SSL_free( response->value );
|
||||
|
||||
|
||||
|
||||
// SSL_CTX_free( this->sslContext );
|
||||
// close( this->socket );
|
||||
|
||||
|
||||
}
|
||||
|
||||
void dump_buffer(void *buffer, int buffer_size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0;i < buffer_size;++i){
|
||||
|
||||
printf( "%c", ( (char *) buffer )[i]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
//kill -9 $(lsof -t -i tcp:8080)
|
||||
|
||||
504
application/source/http.c.old
Normal file
504
application/source/http.c.old
Normal file
@@ -0,0 +1,504 @@
|
||||
// webserver.c
|
||||
|
||||
|
||||
#include "./fileSystem.h"
|
||||
|
||||
#include "./text.h"
|
||||
|
||||
#include "./mimeTypes.h"
|
||||
|
||||
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <request.h>
|
||||
|
||||
#include <headers.h>
|
||||
|
||||
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
|
||||
|
||||
class http{
|
||||
|
||||
int socket;
|
||||
|
||||
struct sockaddr_in * hostAddress = malloc( sizeof( struct sockaddr_in ) );
|
||||
|
||||
int hostAddresslength = sizeof( struct sockaddr_in );
|
||||
|
||||
struct fileSystem * filesystem = new fileSystem();
|
||||
|
||||
struct mimeTypes * mimetypes = new mimeTypes();
|
||||
|
||||
struct headerManager * headers = new headerManager();
|
||||
|
||||
int useSSL = -1;
|
||||
|
||||
|
||||
SSL_CTX * sslContext;
|
||||
|
||||
|
||||
|
||||
void ( * requestCallback )( struct request * requestInstance, struct text * response );
|
||||
|
||||
|
||||
void createServer( void ( * requestCallback )( request * req, text * response ) ) {
|
||||
|
||||
this->requestCallback = requestCallback;
|
||||
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
this->initializeOpenSSL();
|
||||
|
||||
}
|
||||
|
||||
printf("after initializeOpenSSL\n\n");
|
||||
|
||||
}
|
||||
|
||||
initializeOpenSSL() {
|
||||
|
||||
// OpenSSL 1.1.0 or above initializes by itself
|
||||
//SSL_load_error_strings();
|
||||
//ssl_load_ciphers();
|
||||
//OpenSSL_add_all_algorithms();
|
||||
|
||||
/* Lets get nice error messages */
|
||||
SSL_load_error_strings();
|
||||
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
SSLeay_add_ssl_algorithms();
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
int listen( int port ) {
|
||||
|
||||
this->socket = socket( AF_INET, SOCK_STREAM, 0 );
|
||||
|
||||
int iSetOption = 1;
|
||||
|
||||
setsockopt( this->socket, SOL_SOCKET, SO_REUSEADDR, ( char * ) & iSetOption, sizeof( iSetOption ) );
|
||||
|
||||
if ( this->socket == -1 ) {
|
||||
|
||||
perror("webserver (socket)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("socket created successfully\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Create the address to bind the socket to
|
||||
this->hostAddress->sin_family = AF_INET;
|
||||
|
||||
this->hostAddress->sin_port = htons( port );
|
||||
|
||||
this->hostAddress->sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
|
||||
|
||||
// Bind the socket to the address
|
||||
if ( bind( this->socket, ( struct sockaddr * ) this->hostAddress, this->hostAddresslength ) != 0 ) {
|
||||
|
||||
perror("webserver (bind)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("socket successfully bound to address\n");
|
||||
|
||||
}
|
||||
|
||||
// Listen for incoming connections
|
||||
if ( listen( this->socket, SOMAXCONN ) != 0 ) {
|
||||
|
||||
perror("webserver (listen)");
|
||||
|
||||
exit( 0 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("server listening for connections\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (;;) {
|
||||
|
||||
this->acceptConnection( );
|
||||
|
||||
}
|
||||
|
||||
|
||||
//this->DestroySSL();
|
||||
|
||||
printf("exit");
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
char * getExtension( char * path ) {
|
||||
|
||||
array * parts = path->split(".");
|
||||
|
||||
int count = parts->length();
|
||||
|
||||
return parts->get( count - 1 );
|
||||
|
||||
}
|
||||
|
||||
logRequest( request * requestInstance ) {
|
||||
|
||||
printf("server address: [%s:%u]\n", requestInstance->address, requestInstance->port );
|
||||
|
||||
printf("mimeType: %-30s \n", requestInstance->mimeType);
|
||||
|
||||
printf("header: %-30s \n", requestInstance->extension );
|
||||
|
||||
printf("method: %-30s \n", requestInstance->method );
|
||||
|
||||
printf("version: %-30s \n\n", requestInstance->version );
|
||||
|
||||
}
|
||||
|
||||
acceptConnection( ) {
|
||||
|
||||
fileSystem * filesystem = this->filesystem;
|
||||
|
||||
mimeTypes * mimetypes = this->mimetypes;
|
||||
|
||||
text * response = new text("");
|
||||
|
||||
|
||||
|
||||
char buffer[ 4096 ];
|
||||
|
||||
// Create client address
|
||||
struct sockaddr_in client_addr;
|
||||
|
||||
int client_addrlen = sizeof(client_addr);
|
||||
|
||||
// Accept incoming connections
|
||||
int socketConnection = accept( this->socket,
|
||||
(struct sockaddr *)this->hostAddress,
|
||||
( socklen_t * ) & this->hostAddresslength );
|
||||
|
||||
|
||||
/*
|
||||
|
||||
struct rusage r_usage;
|
||||
|
||||
getrusage(RUSAGE_SELF,&r_usage);
|
||||
// Print the maximum resident set size used (in kilobytes).
|
||||
|
||||
printf("Memory usage: %ld kilobytes\n\n",r_usage.ru_maxrss);
|
||||
|
||||
free( r_usage );
|
||||
*/
|
||||
|
||||
SSL * ssl = NULL;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
const SSL_METHOD * method = SSLv23_server_method();
|
||||
|
||||
this->sslContext = SSL_CTX_new( method );
|
||||
|
||||
if ( this->sslContext == NULL ) {
|
||||
|
||||
printf("Error loading SSL_CTX_NEW '%s'\n\n", stderr);
|
||||
|
||||
}
|
||||
|
||||
SSL_CTX_set_options( this->sslContext, SSL_OP_SINGLE_DH_USE );
|
||||
|
||||
int use_cert = SSL_CTX_use_certificate_file( this->sslContext, "/etc/letsencrypt/live/unifyjs.org/fullchain.pem" , SSL_FILETYPE_PEM );
|
||||
|
||||
int use_prv = SSL_CTX_use_PrivateKey_file( this->sslContext, "/etc/letsencrypt/live/unifyjs.org/privkey.pem", SSL_FILETYPE_PEM );
|
||||
|
||||
if( use_cert != 1 ) {
|
||||
|
||||
printf( "error: SSL_CTX_use_certificate_file\n\n" );
|
||||
|
||||
}
|
||||
|
||||
if( use_prv != 1 ) {
|
||||
|
||||
printf( "error: SSL_CTX_use_PrivateKey_file\n\n" );
|
||||
|
||||
}
|
||||
|
||||
if ( !SSL_CTX_check_private_key(this->sslContext) ) {
|
||||
|
||||
printf("Private key does not match the certificate public key\n");
|
||||
|
||||
// initiate page describing error.
|
||||
}
|
||||
|
||||
ssl = SSL_new( this->sslContext );
|
||||
|
||||
SSL_set_fd( ssl, socketConnection );
|
||||
|
||||
//Here is the SSL Accept portion. Now all reads and writes must use SSL
|
||||
int ssl_err = SSL_accept( ssl ); // SSL routines:SSL_UNDEFINED_FUNCTION:called a function you should not call
|
||||
|
||||
//printf ("SSL connection using %s\n", SSL_get_cipher( ssl ) );
|
||||
|
||||
//ERR_print_errors_fp( stderr );
|
||||
|
||||
if( ssl_err == 0 ){
|
||||
|
||||
printf("SSL_accept returned zero\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
int n;
|
||||
|
||||
if( ssl_err < 0 ) {
|
||||
|
||||
int err;
|
||||
|
||||
if( ( err = SSL_get_error(ssl,n ) ) == SSL_ERROR_WANT_READ) {
|
||||
|
||||
printf("SSL_accept wants more data\n");
|
||||
|
||||
return ;
|
||||
|
||||
}
|
||||
|
||||
exit(7);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
printf ("SSL connection using %s\n", SSL_get_cipher (this->sslContext));
|
||||
|
||||
#define CHK_NULL(x) if ((x)==NULL) exit (1)
|
||||
|
||||
X509* client_cert;
|
||||
|
||||
char* str;
|
||||
|
||||
// Get client's certificate (note: beware of dynamic allocation) - opt
|
||||
|
||||
client_cert = SSL_get_peer_certificate (this->sslContext);
|
||||
|
||||
if ( client_cert != NULL ) {
|
||||
|
||||
printf ("Client certificate:\n");
|
||||
|
||||
str = X509_NAME_oneline (X509_get_subject_name (client_cert), 0, 0);
|
||||
|
||||
CHK_NULL(str);
|
||||
|
||||
printf ("\t subject: %s\n", str);
|
||||
|
||||
OPENSSL_free (str);
|
||||
|
||||
str = X509_NAME_oneline (X509_get_issuer_name (client_cert), 0, 0);
|
||||
|
||||
CHK_NULL(str);
|
||||
|
||||
printf ("\t issuer: %s\n", str);
|
||||
|
||||
OPENSSL_free (str);
|
||||
|
||||
X509_free (client_cert);
|
||||
|
||||
} else {
|
||||
|
||||
printf ("Client does not have certificate.\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ( socketConnection == -1 ) {
|
||||
|
||||
perror("webserver (accept)");
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
//printf("connection accepted2\n\n");
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
printf("connection accepted\n\n");
|
||||
|
||||
// Get client address
|
||||
int sockn = getsockname( socketConnection,
|
||||
( struct sockaddr * ) &client_addr,
|
||||
( socklen_t * ) &client_addrlen);
|
||||
|
||||
|
||||
if ( sockn == -1 ) {
|
||||
|
||||
perror("webserver (getsockname)");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int valread;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
valread = SSL_read( ssl, buffer, 4096 - 1 );
|
||||
|
||||
} else {
|
||||
|
||||
printf("read without ssl\n\n");
|
||||
|
||||
int valread = read( socketConnection, buffer, 4096 -1 );
|
||||
|
||||
if ( valread == -1 ) {
|
||||
|
||||
perror("webserver (read)");
|
||||
|
||||
//return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( valread == -1 ) {
|
||||
|
||||
perror("webserver (read)");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
request * requestInstance = new request();
|
||||
|
||||
|
||||
sscanf( buffer, "%s %s %s", requestInstance->method, requestInstance->url, requestInstance->version );
|
||||
|
||||
|
||||
requestInstance->address = inet_ntoa( client_addr.sin_addr );
|
||||
|
||||
requestInstance->port = ntohs( client_addr.sin_port );
|
||||
|
||||
requestInstance->extension = this->getExtension( requestInstance->url );
|
||||
|
||||
requestInstance->mimeType = mimetypes->getByExtension( requestInstance->extension );
|
||||
|
||||
this->requestCallback( requestInstance, response );
|
||||
|
||||
|
||||
|
||||
|
||||
//printf("response: %s",response->value);
|
||||
|
||||
int writeResponse;
|
||||
|
||||
if( this->useSSL == 1 ) {
|
||||
|
||||
writeResponse = SSL_write( ssl, response->value, response->length );
|
||||
|
||||
} else {
|
||||
|
||||
// Write to the socket
|
||||
int writeResponse = write( socketConnection, response->value, response->length );
|
||||
|
||||
printf("close connection");
|
||||
|
||||
if ( writeResponse == -1 ) {
|
||||
|
||||
perror("webserver (write)");
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( writeResponse == -1 ) {
|
||||
|
||||
perror("webserver (write)");
|
||||
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
//printf("written a response\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
//free( buffer );
|
||||
|
||||
//SSL_free( writeResponse );
|
||||
|
||||
//response->free();
|
||||
|
||||
//requestInstance->free();
|
||||
|
||||
close( socketConnection );
|
||||
|
||||
//SSL_free( response->value );
|
||||
|
||||
|
||||
|
||||
// SSL_CTX_free( this->sslContext );
|
||||
// close( this->socket );
|
||||
|
||||
|
||||
}
|
||||
|
||||
void dump_buffer(void *buffer, int buffer_size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0;i < buffer_size;++i){
|
||||
|
||||
printf( "%c", ( (char *) buffer )[i]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void abort() {
|
||||
|
||||
|
||||
}
|
||||
//kill -9 $(lsof -t -i tcp:8080)
|
||||
|
||||
22
application/source/int.c
Executable file
22
application/source/int.c
Executable file
@@ -0,0 +1,22 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class int{
|
||||
|
||||
char * toText( ) {
|
||||
|
||||
char * textNumber = malloc( sizeof( char ) * 20 );
|
||||
|
||||
sprintf( textNumber, "%d", this );
|
||||
|
||||
return textNumber;
|
||||
|
||||
}
|
||||
|
||||
int negative() {
|
||||
|
||||
return 12;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
167
application/source/integerArray.c
Normal file
167
application/source/integerArray.c
Normal file
@@ -0,0 +1,167 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <char.h>
|
||||
|
||||
#include <text.h>
|
||||
|
||||
#include <vector3.h>
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
class integerArray{
|
||||
|
||||
int capacity = 10;
|
||||
|
||||
int total = 0;
|
||||
|
||||
int * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
float get( int index )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
void set( int index, float item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
int * items = realloc( this->items, sizeof( int ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void addVector2( struct vector2 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
}
|
||||
|
||||
void addVector3( struct vector3 * item ) {
|
||||
|
||||
this->add( item->x );
|
||||
|
||||
this->add( item->y );
|
||||
|
||||
this->add( item->z );
|
||||
|
||||
}
|
||||
|
||||
void add( int item )
|
||||
{
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
this->items[ this->total++ ] = item;
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
this->items[index] = 0.0;
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = 0.0;
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int array_push( int item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( int item ) {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
this->total++;
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
for ( int i = length - 1; i >= 0; --i ) {
|
||||
|
||||
this->items[ i + 1 ] = this->items[ i ];
|
||||
|
||||
}
|
||||
|
||||
this->items[ 0 ] = item;
|
||||
|
||||
}
|
||||
|
||||
int pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
int lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
66
application/source/mimeTypes.c
Normal file
66
application/source/mimeTypes.c
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
#include "array.h"
|
||||
|
||||
|
||||
|
||||
class mimeTypes{
|
||||
|
||||
array * extensions = new array();
|
||||
|
||||
array * mimeTypes = new array();
|
||||
|
||||
constructor() {
|
||||
|
||||
this->add("html", "text/html");
|
||||
|
||||
this->add("css", "text/css");
|
||||
|
||||
this->add("ttf", "application/x-font-ttf");
|
||||
|
||||
this->add("png", "image/png");
|
||||
|
||||
this->add("svg", "image/svg+xml");
|
||||
|
||||
}
|
||||
|
||||
void add( char * extension, char * mimeType ) {
|
||||
|
||||
struct array * extensions = this->extensions;
|
||||
|
||||
extensions->add( extension );
|
||||
|
||||
|
||||
struct array * mimeTypes = this->mimeTypes;
|
||||
|
||||
mimeTypes->add( mimeType );
|
||||
|
||||
}
|
||||
|
||||
char * getByExtension( char * extension ) {
|
||||
|
||||
struct array * extensions = this->extensions;
|
||||
|
||||
struct array * mimeTypes = this->mimeTypes;
|
||||
|
||||
int count = extensions->length();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
|
||||
char * currentExtension = extensions->get( i );
|
||||
|
||||
if( currentExtension == extension ) {
|
||||
|
||||
char * mimeType = mimeTypes->get( i );
|
||||
|
||||
return mimeType;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return "no-mimetype";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
13
application/source/reflection.c
Normal file
13
application/source/reflection.c
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
class reflection{
|
||||
|
||||
test() {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
61
application/source/request.c
Normal file
61
application/source/request.c
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
#include "headers.h"
|
||||
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
class request{
|
||||
|
||||
char * connection = malloc( BUFFER_SIZE );
|
||||
|
||||
char * address = malloc( BUFFER_SIZE );
|
||||
|
||||
int port;
|
||||
|
||||
char * url = malloc( BUFFER_SIZE );
|
||||
|
||||
char * method = malloc( BUFFER_SIZE );
|
||||
|
||||
char * version = malloc( BUFFER_SIZE );
|
||||
|
||||
char * mimeType = malloc( BUFFER_SIZE );
|
||||
|
||||
char * extension = malloc( BUFFER_SIZE );
|
||||
|
||||
struct headerManager * headers = new headerManager();
|
||||
|
||||
void constructor() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
free() {
|
||||
|
||||
|
||||
/*
|
||||
free( this->connection );
|
||||
|
||||
free( this->address );
|
||||
|
||||
free( this->port );
|
||||
|
||||
free( this->url );
|
||||
|
||||
free( this->method );
|
||||
|
||||
free( this->version );
|
||||
|
||||
free( this->mimeType );
|
||||
|
||||
free( this->headers );
|
||||
|
||||
*/
|
||||
|
||||
free( this );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
817
application/source/sqlite.c
Executable file
817
application/source/sqlite.c
Executable file
@@ -0,0 +1,817 @@
|
||||
|
||||
|
||||
#include <classConfiguration.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <array.h>
|
||||
|
||||
#include <text.h>
|
||||
|
||||
#include "console.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class sqlite{
|
||||
|
||||
|
||||
sqlite3 * db;
|
||||
|
||||
sqlite3_stmt * res;
|
||||
|
||||
char * value;
|
||||
|
||||
char * selectedModel;
|
||||
|
||||
|
||||
void constructor( char * filePath )
|
||||
{
|
||||
this->value = "good";
|
||||
|
||||
this.connect( filePath );
|
||||
|
||||
}
|
||||
|
||||
void connect( char * filePath ) {
|
||||
|
||||
printf("\n\ntrying to connect.. %s\n\n", filePath);
|
||||
|
||||
int rc = sqlite3_open( filePath, & this->db );
|
||||
|
||||
if ( rc != SQLITE_OK ) {
|
||||
|
||||
printf( "Cannot open database: %s\n", sqlite3_errmsg( this->db ) );
|
||||
|
||||
sqlite3_close( this->db );
|
||||
|
||||
console->error( ( char * ) sqlite3_errmsg( this->db ) );
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
rc = sqlite3_prepare_v2( this->db, "SELECT SQLITE_VERSION()", -1, & this->res, 0 );
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
|
||||
console->error( ( char * ) sqlite3_errmsg( this->db ) );
|
||||
|
||||
sqlite3_close( this->db );
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
rc = sqlite3_step(this->res);
|
||||
|
||||
/*if ( rc == SQLITE_ROW ) {
|
||||
|
||||
printf( "%s\n", ( char * ) sqlite3_column_text( this->res, 0 ) );
|
||||
|
||||
}
|
||||
|
||||
printf( "Success \n");
|
||||
*/
|
||||
//return 1;
|
||||
|
||||
}
|
||||
|
||||
void free() {
|
||||
|
||||
sqlite3_finalize( this->res );
|
||||
|
||||
sqlite3_close( this->db );
|
||||
|
||||
}
|
||||
|
||||
void selectModel( char * className ) {
|
||||
|
||||
this->selectedModel = className;
|
||||
|
||||
/*
|
||||
|
||||
printf("select className: %s\n", className);
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
printf("class index: %i\n", classIndex);
|
||||
|
||||
|
||||
int propertyIndex = getPropertyIndexByPropertyName( classIndex, "username" );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
char ** propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
|
||||
printf("propertyIndex: %i\n\n", propertyIndex);
|
||||
|
||||
for (int i = 0; i < propertyCount; ++i)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[i];
|
||||
|
||||
int propertyOffset = propertyOffsets[i];
|
||||
|
||||
|
||||
printf("propertyName: %s offset: %i\n", propertyName, propertyOffset);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
char * shortPointer = malloc( sizeof( char * ) * 100000 );
|
||||
|
||||
void * * voidArray = malloc( sizeof( void * ) * 100000 );
|
||||
|
||||
int structByteSize;
|
||||
|
||||
getArrayByClassIndex( 100000, voidArray, shortPointer, &structByteSize, 0 );
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
void createTable() {
|
||||
|
||||
char * errorMessage = 0;
|
||||
|
||||
char *sql = "DROP TABLE IF EXISTS user;"
|
||||
"CREATE TABLE user( id INT, username TEXT, userlevel INT, hash TEXT );";
|
||||
|
||||
int rc = sqlite3_exec(this->db, sql, 0, 0, &errorMessage);
|
||||
|
||||
if ( rc != SQLITE_OK ) {
|
||||
|
||||
printf( "SQL error: %s\n", errorMessage );
|
||||
|
||||
sqlite3_free( errorMessage );
|
||||
|
||||
sqlite3_close(this->db);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
// Fast but unsave -> sql injection
|
||||
void addRowsFast( array * insertArray ) {
|
||||
|
||||
array * copy = insertArray;
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
text * query = new text( "" );
|
||||
|
||||
query->append( "INSERT INTO " );
|
||||
|
||||
query->append( this->selectedModel );
|
||||
|
||||
|
||||
sqlite3_stmt * result;
|
||||
|
||||
|
||||
char * * propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
|
||||
|
||||
query->append( " ( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// join table because this is an class
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( propertyName );
|
||||
|
||||
}
|
||||
|
||||
query->append( " ) " );
|
||||
|
||||
query->append(" VALUES " );
|
||||
|
||||
|
||||
|
||||
int insertItemCount = copy->total;
|
||||
|
||||
char * voidArray = ( char * ) copy->items;
|
||||
|
||||
//sqlite3_exec(result, "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||
|
||||
for (int i = 0; i < insertItemCount; ++i)
|
||||
{
|
||||
char * pointer = copy->get( i );
|
||||
|
||||
|
||||
|
||||
if( i > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( "( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// join table because this is an class
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
int propertyOffset = propertyOffsets[propertyIndex];
|
||||
|
||||
int propertyDatatypeIndex = getPropertyDatatypeIndex( datatypeIndices, propertyIndex );
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
if( propertyDatatypeIndex == -5 ) {
|
||||
|
||||
int value = *( int * )(pointer + propertyOffset);
|
||||
|
||||
char * textNumber = malloc( sizeof( char ) * 20 );
|
||||
|
||||
// Boost Spirit.Karma is faster
|
||||
sprintf( textNumber, "%d", value );
|
||||
|
||||
query->append( textNumber );
|
||||
|
||||
} else if( propertyDatatypeIndex == -3 ) {
|
||||
|
||||
char * columnValueCopy = ( char * ) malloc( 8 );
|
||||
|
||||
char * value = ( char * ) ( pointer + propertyOffset );
|
||||
|
||||
strncpy( &columnValueCopy, value, 8 );
|
||||
|
||||
query->append( "'" );
|
||||
|
||||
query->append( columnValueCopy );
|
||||
|
||||
query->append( "'" );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
query->append( " )" );
|
||||
|
||||
}
|
||||
|
||||
//sqlite3_exec(result, "END TRANSACTION;", NULL, NULL, NULL);
|
||||
sqlite3_prepare_v2( this->db, query->buffer, -1, &result, 0 );
|
||||
|
||||
sqlite3_step( result );
|
||||
|
||||
sqlite3_finalize( result );
|
||||
|
||||
//printf("insert query: %s\n", query->buffer);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void addRows( array * insertArray ) {
|
||||
|
||||
array * copy = insertArray;
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
text * query = new text( "" );
|
||||
|
||||
query->append( "INSERT INTO " );
|
||||
|
||||
query->append( this->selectedModel );
|
||||
|
||||
|
||||
sqlite3_stmt * result;
|
||||
|
||||
|
||||
char * * propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
|
||||
|
||||
query->append( " ( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// join table because this is an class
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( propertyName );
|
||||
|
||||
}
|
||||
|
||||
query->append( " ) " );
|
||||
|
||||
query->append( " VALUES ( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// join table because this is an class
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( "?" );
|
||||
|
||||
}
|
||||
|
||||
query->append(" ) " );
|
||||
|
||||
|
||||
|
||||
sqlite3_prepare_v2( this->db, query->value, -1, &result, 0 );
|
||||
|
||||
sqlite3_exec(this->db, this->selectedModel, NULL, NULL, 0);
|
||||
|
||||
sqlite3_exec(this->db, "PRAGMA synchronous = OFF", NULL, NULL, 0);
|
||||
|
||||
sqlite3_exec(this->db, "PRAGMA journal_mode = MEMORY", NULL, NULL, 0);
|
||||
|
||||
|
||||
int insertItemCount = copy->total;
|
||||
|
||||
char * voidArray = ( char * ) copy->items;
|
||||
|
||||
//sqlite3_exec(result, "BEGIN TRANSACTION;", NULL, NULL, NULL);
|
||||
|
||||
for (int i = 0; i < insertItemCount; ++i)
|
||||
{
|
||||
char * pointer = copy->get( i );
|
||||
|
||||
this->updateRow( result, propertyCount, propertyNames, propertyOffsets, datatypeIndices, pointer );
|
||||
|
||||
sqlite3_step( result );
|
||||
|
||||
sqlite3_reset( result );
|
||||
|
||||
}
|
||||
|
||||
//sqlite3_exec(result, "END TRANSACTION;", NULL, NULL, NULL);
|
||||
|
||||
sqlite3_finalize( result );
|
||||
|
||||
//printf("insert query: %s\n", query->buffer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void updateRow( sqlite3_stmt * result, int propertyCount, char * * propertyNames, int * propertyOffsets, int * datatypeIndices, char * pointer ) {
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
int propertyOffset = propertyOffsets[propertyIndex];
|
||||
|
||||
int propertyDatatypeIndex = getPropertyDatatypeIndex( datatypeIndices, propertyIndex );
|
||||
|
||||
//printf("propertyName: %s offset: %i datatype: %i propertyIndex: %i\n", propertyName, propertyOffset, propertyDatatypeIndex, propertyIndex);
|
||||
|
||||
if( propertyDatatypeIndex == -5 ) {
|
||||
|
||||
int value = *( int * )(pointer + propertyOffset);
|
||||
|
||||
//printf("integer value: %i %i\n", propertyIndex + 1, value);
|
||||
|
||||
sqlite3_bind_int( result, propertyIndex + 1, value );
|
||||
|
||||
} else if( propertyDatatypeIndex == -3 ) {
|
||||
|
||||
//char * columnValueCopy = ( char * ) malloc( 8 );
|
||||
|
||||
uintptr_t * value = ( uintptr_t * ) ( pointer + propertyOffset );
|
||||
|
||||
//strncpy( &columnValueCopy, value, 8 );
|
||||
|
||||
//printf("char * value: %i %s\n", propertyIndex + 1, columnValueCopy);
|
||||
|
||||
sqlite3_bind_text( result, propertyIndex + 1, (char *) *value, -1, SQLITE_TRANSIENT );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//printf("\n\n\n");
|
||||
|
||||
}
|
||||
|
||||
void update( void * row ) {
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
char ** propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
int propertyIdOfIndex = -1;
|
||||
|
||||
|
||||
for ( int i = 0; i < propertyCount; ++i )
|
||||
{
|
||||
char * propertyName = propertyNames[i];
|
||||
|
||||
//printf("propertyName: %s\n", propertyName);
|
||||
|
||||
if( strcmp( propertyName, "id" ) == 0 ) {
|
||||
|
||||
propertyIdOfIndex = i;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if( propertyIdOfIndex == -1 ) {
|
||||
|
||||
printf("Class '%s' does not have an id field. ");
|
||||
|
||||
}
|
||||
|
||||
int idOffset = propertyOffsets[ propertyIdOfIndex ];
|
||||
|
||||
char * pointer = row;
|
||||
|
||||
int id = *( pointer + idOffset );
|
||||
|
||||
text * query = new text( "" ); //"UPDATE user set username = '?' where id=?; "
|
||||
|
||||
query->append( "UPDATE " );
|
||||
|
||||
query->append( this->selectedModel );
|
||||
|
||||
query->append( " SET " );
|
||||
|
||||
int activePropertyCount = 0;
|
||||
|
||||
// set propertyCount
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[propertyIndex];
|
||||
|
||||
int datatypeIndex = datatypeIndices[propertyIndex];
|
||||
|
||||
// todo properties that are classes like "array"
|
||||
if( datatypeIndex > 0 ) {
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
activePropertyCount++;
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( propertyName );
|
||||
|
||||
query->append( " = ? " );
|
||||
|
||||
}
|
||||
|
||||
query->append( " where id = ? " );
|
||||
|
||||
sqlite3_stmt * result;
|
||||
|
||||
sqlite3_prepare_v2( this->db, query->value, -1, &result, 0 );
|
||||
|
||||
this->updateRow( result, propertyCount, propertyNames, propertyOffsets, datatypeIndices, pointer );
|
||||
|
||||
sqlite3_bind_int( result, activePropertyCount + 1, id );
|
||||
|
||||
//printf("propertyCount: %i %i\n", propertyCount, id );
|
||||
|
||||
sqlite3_step( result );
|
||||
|
||||
sqlite3_finalize( result );
|
||||
|
||||
//printf("update query: %s\n", query->buffer);
|
||||
|
||||
}
|
||||
|
||||
void addRow( void * row ) {
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
text * query = new text( "" );
|
||||
|
||||
query->append( "INSERT INTO " );
|
||||
|
||||
query->append( this->selectedModel );
|
||||
|
||||
|
||||
sqlite3_stmt * result;
|
||||
|
||||
|
||||
char * * propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
char * pointer = row;
|
||||
|
||||
|
||||
query->append( " ( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
char * propertyName = propertyNames[ propertyIndex ];
|
||||
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// join table because this is an class
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( propertyName );
|
||||
|
||||
}
|
||||
|
||||
query->append( " ) " );
|
||||
|
||||
query->append( " VALUES ( " );
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
|
||||
{
|
||||
|
||||
int datatype = datatypeIndices[ propertyIndex ];
|
||||
|
||||
if( datatype > 0 ) {
|
||||
|
||||
// set joined table id
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
|
||||
if( propertyIndex > 0 ) {
|
||||
|
||||
query->append( ", " );
|
||||
|
||||
}
|
||||
|
||||
query->append( "?" );
|
||||
|
||||
}
|
||||
|
||||
query->append(" ) " );
|
||||
|
||||
|
||||
|
||||
// {
|
||||
|
||||
sqlite3_prepare_v2( this->db, query->value, -1, &result, 0 );
|
||||
|
||||
this->updateRow( result, propertyCount, propertyNames, propertyOffsets, datatypeIndices, pointer );
|
||||
|
||||
sqlite3_step( result );
|
||||
|
||||
// }
|
||||
|
||||
sqlite3_finalize( result );
|
||||
|
||||
//printf("query: %s\n", query->buffer);
|
||||
|
||||
}
|
||||
|
||||
struct array * fetchRows( char * sql ) {
|
||||
|
||||
sqlite3_stmt * result;
|
||||
|
||||
int rc = sqlite3_prepare_v2( this->db, sql, -1, &result, 0 );
|
||||
|
||||
if ( rc != SQLITE_OK ) {
|
||||
|
||||
printf("Failed to execute statement: %s\n", sqlite3_errmsg( this->db ) );
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
if ( rc != SQLITE_ROW ) {
|
||||
|
||||
printf("not rows found\n\n");
|
||||
|
||||
}
|
||||
*/
|
||||
int rowIndex = 0;
|
||||
|
||||
|
||||
//printf("select className: %s\n", this->selectedModel);
|
||||
|
||||
int classIndex = getClassIndexByClassName( this->selectedModel );
|
||||
|
||||
|
||||
//printf("class index: %i\n", classIndex);
|
||||
|
||||
|
||||
int propertyIndex = getPropertyIndexByPropertyName( classIndex, "username" );
|
||||
|
||||
int propertyCount = getPropertyCountByClassIndex( classIndex );
|
||||
|
||||
char ** propertyNames = getPropertiesByClassIndex( classIndex );
|
||||
|
||||
int * propertyOffsets = getPropertyOffsetsByClassIndex( classIndex );
|
||||
|
||||
int * datatypeIndices = getPropertyDatatypeIndexesByClassIndex( classIndex );
|
||||
|
||||
|
||||
char * shortPointer = malloc( sizeof( char * ) * 100000 );
|
||||
|
||||
void * * voidArray = malloc( sizeof( void * ) * 100000 );
|
||||
|
||||
int structByteSize;
|
||||
|
||||
getArrayByClassIndex( 1000, voidArray, &structByteSize, classIndex );
|
||||
|
||||
shortPointer = ( char * ) shortPointer;
|
||||
|
||||
|
||||
while ( sqlite3_step( result ) != SQLITE_DONE ) {
|
||||
|
||||
//if( rowsCount == 0 ) {
|
||||
|
||||
int pointerIndex = ( rowIndex * ( structByteSize ) );
|
||||
|
||||
//==printf("pointerIndex: %i\n", pointerIndex);
|
||||
|
||||
char * pointer = shortPointer + pointerIndex;
|
||||
|
||||
int columnCount = sqlite3_data_count( result );
|
||||
|
||||
//printf("columnCount: %i\n\n", columnCount);
|
||||
|
||||
for (int i = 0; i < columnCount; ++i)
|
||||
{
|
||||
|
||||
const char * columnName = sqlite3_column_name( result, i );
|
||||
|
||||
const int propertyIndex = getPropertyIndexByPropertyName( classIndex, (char *)columnName );
|
||||
|
||||
int propertyDatatypeIndex = getPropertyDatatypeIndex( datatypeIndices, propertyIndex );
|
||||
|
||||
int propertyOffset = getPropertyOffsetByPropertyIndex( propertyOffsets, propertyIndex );
|
||||
|
||||
//printf("sqlite3 column index: %i\n", i);
|
||||
|
||||
//printf("sqlite3 columnName: %s\n", columnName);
|
||||
|
||||
//printf("class property index: %i\n", propertyIndex);
|
||||
|
||||
//printf("class property offset: %i\n", propertyOffset);
|
||||
|
||||
if( propertyDatatypeIndex == -5 ) {
|
||||
|
||||
const int columnValue = sqlite3_column_int( result, i );
|
||||
|
||||
|
||||
*( int * )( pointer + propertyOffset ) = columnValue;
|
||||
|
||||
} else if( propertyDatatypeIndex == -3 ) {
|
||||
|
||||
const char * columnValue = sqlite3_column_text( result, i );
|
||||
|
||||
char * columnValueCopy = malloc( strlen( columnValue ) );
|
||||
|
||||
strncpy( columnValueCopy, columnValue, strlen( columnValue ) );
|
||||
|
||||
//columnValueCopy[8] = "\0";
|
||||
|
||||
|
||||
// something wrong here.
|
||||
|
||||
memcpy( pointer + propertyOffset, &columnValueCopy, 8 );
|
||||
|
||||
}
|
||||
|
||||
//printf("class property datatype: %i\n\n\n", propertyDatatypeIndex);
|
||||
|
||||
}
|
||||
|
||||
|
||||
voidArray[rowIndex] = pointer;
|
||||
|
||||
// printf("rowIndex: %i\n", rowIndex);
|
||||
|
||||
rowIndex++;
|
||||
|
||||
}
|
||||
|
||||
struct array * rows = new array();
|
||||
|
||||
rows->items = voidArray;
|
||||
|
||||
rows->total = rowIndex;
|
||||
|
||||
return rows;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
16
application/source/street.c
Normal file
16
application/source/street.c
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
|
||||
class address{
|
||||
|
||||
char * street;
|
||||
|
||||
int number;
|
||||
|
||||
void someMethod() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
178
application/source/text.c
Executable file
178
application/source/text.c
Executable file
@@ -0,0 +1,178 @@
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <array.h>
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
|
||||
|
||||
class text{
|
||||
|
||||
char * value;
|
||||
|
||||
|
||||
int usevalue = -1;
|
||||
|
||||
int length;
|
||||
|
||||
int capacity = 500;
|
||||
|
||||
|
||||
int operator==( text * b ) {
|
||||
|
||||
if( strcmp( this->value, b->value ) == 0 ) {
|
||||
|
||||
return 1;
|
||||
|
||||
} else {
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
text * operator+=( char * b ) {
|
||||
|
||||
this->append( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
void constructor( char * value ) {
|
||||
|
||||
this->length = strlen( value );
|
||||
|
||||
if( this->length > this->capacity ) {
|
||||
|
||||
this->capacity = this->length * 2;
|
||||
|
||||
}
|
||||
|
||||
this->value = malloc( sizeof( char ) * this->capacity );
|
||||
|
||||
strcpy( this->value, value );
|
||||
}
|
||||
|
||||
char get( int index ) {
|
||||
|
||||
return this->value[ index ];
|
||||
|
||||
}
|
||||
|
||||
void resize( int size ) {
|
||||
|
||||
this->value = realloc( this->value, size );
|
||||
|
||||
this->capacity = size;
|
||||
|
||||
}
|
||||
|
||||
text * append( char * value ) {
|
||||
|
||||
int originalLength = this->length;
|
||||
|
||||
int newValueLength = strlen( value );
|
||||
|
||||
this->length += newValueLength;
|
||||
|
||||
if( this->length > this->capacity ) {
|
||||
|
||||
this->resize( this->length * 2 );
|
||||
|
||||
}
|
||||
|
||||
memcpy( this->value + originalLength, value, newValueLength + 1 );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
text * appendBinary( char * value, int size ) {
|
||||
|
||||
int originalLength = this->length;
|
||||
|
||||
int newValueLength = size;
|
||||
|
||||
this->length += newValueLength;
|
||||
|
||||
if( this->length > this->capacity ) {
|
||||
|
||||
this->resize( this->length * 2 );
|
||||
|
||||
}
|
||||
|
||||
memcpy( this->value + originalLength, value, newValueLength + 1 );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
text * appendObject( text * object ) {
|
||||
|
||||
int originalLength = this->length;
|
||||
|
||||
int newValueLength = object->length;
|
||||
|
||||
this->length += newValueLength;
|
||||
|
||||
if( this->length > this->capacity ) {
|
||||
|
||||
this->resize( this->length * 2 );
|
||||
|
||||
}
|
||||
|
||||
memcpy(this->value + originalLength, object->value, newValueLength + 1);
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
text * concatenate( char * value ) {
|
||||
|
||||
text * copy = new text( this->value );
|
||||
|
||||
strcat( copy->value, value );
|
||||
|
||||
return copy;
|
||||
|
||||
}
|
||||
|
||||
char * toNative() {
|
||||
|
||||
return this->value;
|
||||
|
||||
}
|
||||
|
||||
char * whiteSpace( int whiteSpaceCount ) {
|
||||
|
||||
char * output = malloc( 400 );
|
||||
|
||||
for (int i = 0; i < whiteSpaceCount; ++i)
|
||||
{
|
||||
|
||||
strcat( output, " " );
|
||||
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
void free() {
|
||||
|
||||
free( this->value );
|
||||
|
||||
free( this );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
28
application/source/toolset.c
Normal file
28
application/source/toolset.c
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
class toolset{
|
||||
|
||||
void createHorisontalLine() {
|
||||
|
||||
struct winsize w;
|
||||
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
|
||||
for (int i = 0; i < w.ws_col; ++i)
|
||||
{
|
||||
|
||||
printf("-");
|
||||
|
||||
};
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
18
application/source/triangle.c
Executable file
18
application/source/triangle.c
Executable file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
#include <vector2.h>
|
||||
|
||||
class triangle{
|
||||
|
||||
vector2 * a = new vector2( 1, 2 );
|
||||
|
||||
vector2 b = new vector2( 3, 400 );
|
||||
|
||||
vector2 c = new vector2( 5, 6 );
|
||||
|
||||
add( triangle * a ) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
24
application/source/user.c
Normal file
24
application/source/user.c
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#include <array.h>
|
||||
|
||||
class user{
|
||||
|
||||
char * username;
|
||||
|
||||
int id;
|
||||
|
||||
int userlevel;
|
||||
|
||||
char * hash;
|
||||
|
||||
struct array * addresses = new array();
|
||||
|
||||
constructor() {
|
||||
|
||||
//printf("create array();\n");
|
||||
|
||||
//this->addresses = new array();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
72
application/source/vector2.c
Executable file
72
application/source/vector2.c
Executable file
@@ -0,0 +1,72 @@
|
||||
|
||||
|
||||
|
||||
|
||||
class vector2{
|
||||
|
||||
float x;
|
||||
|
||||
float y;
|
||||
|
||||
|
||||
constructor( float x, float y ) {
|
||||
|
||||
//printf("constructor has been called.. x: %i, y: %i \n", x, y);
|
||||
|
||||
this->x = x;
|
||||
|
||||
this->y = y;
|
||||
|
||||
// this causes an infinity loop.
|
||||
// vector2 a = new vector2( 0, 0 );
|
||||
|
||||
// this->add( &a );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector2 * operator+( vector2 * b ){
|
||||
|
||||
this->add( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
vector2 * operator+=( struct vector2 * b ) {
|
||||
|
||||
// not proper
|
||||
//strcat( this, b );
|
||||
|
||||
//printf("operator += triggered");
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
add( vector2 * a ) {
|
||||
|
||||
this->x += a->x;
|
||||
|
||||
this->y += a->y;
|
||||
|
||||
}
|
||||
|
||||
subtract( vector2 * a ) {
|
||||
|
||||
this->x -= a->x;
|
||||
|
||||
this->y -= a->y;
|
||||
|
||||
}
|
||||
|
||||
int length() {
|
||||
|
||||
return this->x + this->y;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
53
application/source/vector3.c
Normal file
53
application/source/vector3.c
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
|
||||
|
||||
|
||||
class vector3{
|
||||
|
||||
float x;
|
||||
|
||||
float y;
|
||||
|
||||
float z;
|
||||
|
||||
|
||||
|
||||
constructor( float x, float y, float z ) {
|
||||
|
||||
this->x = x;
|
||||
|
||||
this->y = y;
|
||||
|
||||
this->z = z;
|
||||
|
||||
}
|
||||
|
||||
vector3 * operator+( vector3 * b ) {
|
||||
|
||||
this->add( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
vector3 * operator+=( vector3 * b ) {
|
||||
|
||||
this->add( b );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void add( vector3 * b ) {
|
||||
|
||||
this->x += b->x;
|
||||
|
||||
this->y += b->y;
|
||||
|
||||
this->z += b->z;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user