Permalink
Browse files

sistema funcional com threads, porem renderizando com erro

  • Loading branch information...
1 parent 41eef21 commit b89b2767cc48df89208f18247a4af9f4af21ee7c @prsolucoes committed Sep 17, 2012
Showing with 350 additions and 5 deletions.
  1. +2 −0 CMakeLists.txt
  2. +69 −3 Character.cpp
  3. +2 −0 Character.h
  4. +188 −0 Engine.cpp
  5. +50 −0 Engine.h
  6. +12 −0 Util.cpp
  7. +16 −0 Util.h
  8. +5 −0 main.cpp
  9. +6 −2 sfml-cmake.pro
View
@@ -11,6 +11,8 @@ set(
../v8wrap.cpp
../JSCharacter.cpp
../Character.cpp
+ ../Engine.cpp
+ ../Util.cpp
)
if(APPLE)
View
@@ -2,7 +2,8 @@
Character::Character()
{
- speed = 2.0f;
+ speed = 2.0f;
+ direction = 1;
}
void Character::setType(std::string type)
@@ -39,12 +40,34 @@ void Character::mssleep(int ms)
void Character::rotateLeft()
{
- sprite->setRotation(sprite->getRotation() - 1.0f);
+ direction--;
+
+ if (direction > 8)
+ {
+ direction = 8;
+ }
+ else if (direction < 1)
+ {
+ direction = 1;
+ }
+
+ sprite->setRotation(getAngleForDirection(direction));
}
void Character::rotateRight()
{
- sprite->setRotation(sprite->getRotation() + 1.0f);
+ direction++;
+
+ if (direction > 8)
+ {
+ direction = 8;
+ }
+ else if (direction < 1)
+ {
+ direction = 1;
+ }
+
+ sprite->setRotation(getAngleForDirection(direction));
}
void Character::loadSprite()
@@ -58,3 +81,46 @@ void Character::loadSprite()
sprite = new sf::Sprite(*texture);
sprite->setPosition(100, 200);
}
+
+int Character::getAngleForDirection(int direction)
+{
+ int angle = 0;
+
+ switch(direction)
+ {
+ case 1: {
+ angle = 0;
+ break;
+ }
+ case 2: {
+ angle = 45;
+ break;
+ }
+ case 3: {
+ angle = 90;
+ break;
+ }
+ case 4: {
+ angle = 135;
+ break;
+ }
+ case 5: {
+ angle = 180;
+ break;
+ }
+ case 6: {
+ angle = 225;
+ break;
+ }
+ case 7: {
+ angle = 270;
+ break;
+ }
+ case 8: {
+ angle = 315;
+ break;
+ }
+ }
+
+ return angle;
+}
View
@@ -30,8 +30,10 @@ class Character
sf::Texture *texture;
std::string type;
float speed;
+ int direction;
void loadSprite();
+ int getAngleForDirection(int direction);
};
View
@@ -0,0 +1,188 @@
+#include "Engine.h"
+
+Engine::Engine()
+{
+ Util::log("Engine::Engine");
+ useThread = true;
+}
+
+Engine::~Engine()
+{
+ Util::log("Engine::~Engine");
+}
+
+void Engine::start()
+{
+ Util::log("Engine::start");
+
+ initializeV8();
+ initializePlayers();
+ initializeV8Binding();
+
+ if (useThread)
+ {
+ runInThread();
+ }
+ else
+ {
+ run();
+ }
+}
+
+void Engine::run()
+{
+ Util::log("Engine::run");
+
+ initializeGraphics();
+
+ sf::Texture texture;
+
+ if (!texture.loadFromFile(resourcePath() + "images/robot1.png"))
+ {
+ throw std::exception();
+ }
+
+ sf::Sprite *sprite = new sf::Sprite(texture);
+
+ while (window->isOpen())
+ {
+ checkEvents();
+
+ window->clear(sf::Color(255, 255, 255));
+ window->draw(*sprite);
+ window->display();
+
+ sleep(1);
+ }
+
+ delete sprite;
+}
+
+void Engine::runInThread()
+{
+ Util::log("Engine::runInThread");
+
+ initializeGraphics();
+
+ sf::Thread thread(&Engine::loadScript, context);
+
+ while (window->isOpen())
+ {
+ checkEvents();
+
+ window->clear(sf::Color(255, 255, 255));
+
+ thread.launch();
+
+ window->draw(*robot1->getSprite());
+ window->draw(*robot2->getSprite());
+ window->display();
+
+ sleep(1);
+ }
+}
+
+void Engine::loadScript(v8::Persistent<v8::Context> &context)
+{
+ Util::log("Engine::loadScript");
+
+ v8::Locker locker;
+
+ v8::Context::Scope scope(context);
+
+ v8::Handle<v8::String> source = readFile(resourcePath() + "/js/robot1.js");
+ v8::Handle<v8::Script> script = v8::Script::Compile(source);
+ v8::Handle<v8::Value> result = script->Run();
+
+ context.Dispose();
+
+ Util::log("Engine::loadScript::end");
+}
+
+v8::Handle<v8::String> Engine::readFile(const std::string& name)
+{
+ Util::log("Engine::readFile");
+
+ FILE* file = fopen(name.c_str(), "rb");
+ if (file == NULL) return v8::Handle<v8::String>();
+
+ fseek(file, 0, SEEK_END);
+ int size = ftell(file);
+ rewind(file);
+
+ char* chars = new char[size + 1];
+ chars[size] = '\0';
+
+ for (int i = 0; i < size;)
+ {
+ int read = fread(&chars[i], 1, size - i, file);
+ i += read;
+ }
+
+ fclose(file);
+ v8::Handle<v8::String> result = v8::String::New(chars, size);
+ delete[] chars;
+ return result;
+}
+
+void Engine::initializeGraphics()
+{
+ Util::log("Engine::initializeGraphics");
+ window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "SFML Sample Application");
+}
+
+void Engine::checkEvents()
+{
+ Util::log("Engine::checkEvents");
+
+ sf::Event Event;
+
+ while (window->pollEvent(Event))
+ {
+ switch (Event.type)
+ {
+ case sf::Event::Closed:
+ window->close();
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+void Engine::initializePlayers()
+{
+ Util::log("Engine::initializePlayers");
+
+ robot1 = new Character();
+ robot1->setType("robot1");
+
+ robot2 = new Character();
+ robot2->setType("robot1");
+
+ robot1->getSprite()->move(Util::randomInt(0, 900), Util::randomInt(0, 500));
+ robot2->getSprite()->move(Util::randomInt(0, 900), Util::randomInt(0, 500));
+}
+
+void Engine::initializeV8()
+{
+ Util::log("Engine::initializeV8");
+
+ context = v8::Context::New();
+ scope = new v8::Context::Scope(context);
+}
+
+void Engine::initializeV8Binding()
+{
+ Util::log("Engine::initializeV8Binding");
+
+ JSCharacter::InitPOT(JSCharacter::POT);
+
+ v8::Handle<v8::Object> Result1 = JSCharacter::POT->NewInstance();
+ JSCharacter::MakeReference(Result1, robot1);
+ context->Global()->Set(v8::String::New("robot1"), Result1);
+
+ v8::Handle<v8::Object> Result2 = JSCharacter::POT->NewInstance();
+ JSCharacter::MakeReference(Result2, robot2);
+ context->Global()->Set(v8::String::New("robot2"), Result2);
+}
View
@@ -0,0 +1,50 @@
+#ifndef ENGINE_H
+#define ENGINE_H
+
+#include "Character.h"
+#include "JSCharacter.h"
+#include "Util.h"
+
+#if __APPLE__
+ #include "ResourcePath.hpp"
+#else
+ std::string resourcePath(void) { return ""; }
+#endif
+
+class Engine
+{
+
+public:
+ Engine();
+ ~Engine();
+
+ void start();
+ void run();
+ void runInThread();
+ static void loadScript(v8::Persistent<v8::Context> &context);
+
+private:
+ Character *robot1;
+ Character *robot2;
+
+ sf::Thread *thread;
+
+ v8::HandleScope handleScope;
+ v8::Persistent<v8::Context> context;
+ v8::Context::Scope *scope;
+
+ bool useThread;
+
+ sf::RenderWindow *window;
+
+ static v8::Handle<v8::String> readFile(const std::string& name);
+
+ void initializeGraphics();
+ void checkEvents();
+ void initializePlayers();
+ void initializeV8();
+ void initializeV8Binding();
+
+};
+
+#endif // ENGINE_H
View
@@ -0,0 +1,12 @@
+#include "Util.h"
+
+int Util::randomInt(int min, int max)
+{
+ return min + (rand() % (int)(max - min + 1));
+}
+
+void Util::log(std::string msg)
+{
+ // show debug log
+ std::cout << "DEBUG: " << msg << std::endl;
+}
View
16 Util.h
@@ -0,0 +1,16 @@
+#ifndef UTIL_H
+#define UTIL_H
+
+#include <stdlib.h>
+#include <iostream>
+
+class Util
+{
+
+public:
+ static int randomInt(int min, int max);
+ static void log(std::string msg);
+
+};
+
+#endif // UTIL_H
Oops, something went wrong.

0 comments on commit b89b276

Please sign in to comment.