Skip to content

Commit

Permalink
Finalização da infraestrutura para o kernel.
Browse files Browse the repository at this point in the history
  • Loading branch information
edsomjr committed Apr 23, 2016
1 parent da420f0 commit 7de0253
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lib
obj
*.d
test/test
*.so*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CFLAGS = -pedantic -std=c++11 -MMD -g3 -g\
-Wsign-promo -Wstrict-null-sentinel -Wswitch-default -Wundef\
-Wzero-as-null-pointer-constant -Wuseless-cast -Wnon-virtual-dtor
INCLUDES = -Iinclude -Itest `sdl2-config --cflags`
LIBS = `sdl2-config --libs` -lSDL2_image -lSDL2_ttf -lSDL2_mixer
LIBS = `sdl2-config --libs` -lSDL2_image -lSDL2_ttf -lSDL2_mixer -ldl

TARGET = $(LIB_DIR)/lib$(NAME).a

Expand Down
9 changes: 9 additions & 0 deletions include/engine.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#ifndef IJENGINE_ENGINE_H
#define IJENGINE_ENGINE_H

#include <memory>

using std::unique_ptr;

namespace ijengine {

class Lib;

class Engine {
public:
Engine();
~Engine();

private:
unique_ptr<Lib> m_kernel_lib;
};

namespace video {
Expand Down
5 changes: 4 additions & 1 deletion include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define IJENGINE_OS_H

#include <string>
#include <list>

using std::string;
using std::list;

namespace ijengine
{
Expand All @@ -12,7 +14,8 @@ namespace ijengine
namespace os
{
Lib * load_lib(const string& path);
}
list<string> list_files(const string& dirpath);
}
}

#endif
11 changes: 6 additions & 5 deletions include/sdl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace ijengine {

class LibSDL2 : public Lib {
public:
~LibSDL2();
LibSDL2() {}
~LibSDL2() {}

string name() const;
string version() const;
string name() const { return ""; }
string version() const { return ""; }

void config(const string& param, const string& value);
void init();
void config(const string& param, const string& value) {}
void init() {}
};

}
Expand Down
3 changes: 2 additions & 1 deletion kernel/sdl2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ all:

$(TARGET): $(OBJ)
@echo Building $@
@$(CC) -shared $(CFLAGS) $(INCLUDES) $(OBJ) -o $@
@echo $(CC) -shared -rdynamic $(CFLAGS) $(INCLUDES) $(OBJ) -o $@
@$(CC) -shared -rdynamic $(CFLAGS) $(INCLUDES) $(OBJ) -o $@ $(LIBS)
@echo Done.

clean:
Expand Down
43 changes: 43 additions & 0 deletions src/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
#include "exception.h"
#include "engine.h"
#include "kernel.h"
#include "lib.h"
#include "os.h"

#include <stdio.h>

namespace ijengine
{
static Kernel *kernel = nullptr;

Engine::Engine()
{
auto files = os::list_files("libs");
string path("");

for (auto file : files)
if (file.find("kernel") != string::npos)
{
path = "libs/" + file;
break;
}

if (path.size() == 0)
throw Exception("Kernel not found in libs dir");

m_kernel_lib = unique_ptr<Lib>(os::load_lib(path));

if (not m_kernel_lib)
throw Exception("Can't load kernel lib");

auto sym = m_kernel_lib->symbol("create_kernel");

if (not sym)
throw Exception("Invalid kernel: missing create_kernel()");

Kernel * (*create)();
*reinterpret_cast<void **>(&create) = sym;

kernel = create();

if (not kernel)
throw Exception("Can't create the kernel");
}

Engine::~Engine()
{
auto sym = m_kernel_lib->symbol("destroy_kernel");

if (not sym)
throw Exception("Invalid kernel: missing destroy_kernel()");

void (*destroy)(Kernel *);
*reinterpret_cast<void **>(&destroy) = sym;

destroy(kernel);
}

namespace video
Expand Down
25 changes: 25 additions & 0 deletions src/linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "exception.h"

#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>

namespace ijengine
{
Expand All @@ -19,5 +22,27 @@ namespace ijengine

return new Lib(handle, dlclose);
}

list<string>
list_files(const string& dirpath)
{
list<string> files;

DIR *dir = opendir(dirpath.c_str());

if (not dir)
return files;

while (auto entry = readdir(dir))
{
if (!strcmp(entry->d_name, ".") or
!strcmp(entry->d_name, ".."))
continue;

files.push_back(entry->d_name);
}

return files;
}
}
}
17 changes: 13 additions & 4 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#include "sdl2game.h"
#include "engine.h"
#include "exception.h"
#include "SDL2/SDL.h"
#include <iostream>

using namespace ijengine;
using namespace std;

int main()
{
video::make_video();

SDL2Game game("Teste", 800, 600);
try
{
Engine engine;
} catch (Exception& ex)
{
cout << ex.what() << endl;
}

return game.run("green");
// SDL2Game game("Teste", 800, 600);

//// return game.run("green");
return 0;
}

0 comments on commit 7de0253

Please sign in to comment.