Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /// test.as ///
  2.  
  3. void ON_EVENT(cEvent &evt)
  4. {
  5.     switch(evt.getType())
  6.     {
  7.     case 1 :
  8.         ON_MOUSE_MOVE(evt.get_mouse_x(), evt.get_mouse_y());
  9.         break;
  10.     case 2 :
  11.         ON_KEY_DOWN(evt.get_key_code());
  12.         break;
  13.     }
  14. }
  15.  
  16. void ON_MOUSE_MOVE(int x, int y)
  17. {
  18.     me.setX(x);
  19.     me.setY(y);
  20. }
  21.  
  22. void ON_KEY_DOWN(int key)
  23. {  
  24.     switch(key)
  25.     {
  26.     case 119 :
  27.         me.setY(me.getY()+0.1f);
  28.         break;
  29.        
  30.     case 100 :
  31.         me.setX(me.getX()+0.1f);
  32.         break;
  33.        
  34.     case 115 :
  35.         me.setY(me.getY()-0.1f);
  36.         break;
  37.        
  38.     case 97 :
  39.         me.setX(me.getX()-0.1f);
  40.         break;
  41.     }
  42. }
  43.  
  44. ///////////////
  45.  
  46. #include <stdio.h>
  47.  
  48. #include "../ScriptLib/ScriptLib/include/ScriptLib.h"
  49.  
  50. #include "cComponent.h"
  51. #include "cEventHandler.h"
  52. #include "cEventListener.h"
  53. #include "cEventRegistry.h"
  54. #include "cEntity.h"
  55. #include "cScriptEventHandler.h"
  56. #include "cEvent.h"
  57. #include "cKeyEvent.h"
  58.  
  59. #include <GLUT/glut.h>
  60.  
  61.  
  62. char keys[255];
  63. cEventRegistry *evt_reg;
  64. cEntity *ent;
  65. int mx;
  66. int my;
  67.  
  68. void push_key_events(cEventRegistry *reg, char keys[256])
  69. {
  70.     for(int a = 0;a < 256;a++)
  71.     {
  72.         if(keys[a]==1)
  73.             reg->push_event(new cEvent(a, DOWN, KEY_EVENT));
  74.     }
  75. }
  76.  
  77. void display(void)
  78. {
  79.     push_key_events(evt_reg, keys);
  80.     evt_reg->process_events();
  81.    
  82.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  83.    
  84.     glPushMatrix();
  85.         glTranslatef(ent->getX(), ent->getY(), 0);
  86.         glBegin(GL_QUADS);
  87.             glVertex2f(-10, -10);
  88.             glVertex2f(10, -10);
  89.             glVertex2f(10, 10);
  90.             glVertex2f(-10, 10);
  91.         glEnd();
  92.     glPopMatrix();
  93.    
  94.     glutSwapBuffers();
  95. }
  96.  
  97. void reshape(int width, int height)
  98. {
  99.     glViewport(0, 0, width, height);
  100.     glMatrixMode(GL_PROJECTION);
  101.     glLoadIdentity();
  102.     gluOrtho2D(0, width, 0, height);
  103.     glMatrixMode(GL_MODELVIEW);
  104. }
  105.  
  106. void idle(void)
  107. {
  108.     glutPostRedisplay();
  109. }
  110.  
  111.  
  112. void KeyDown(unsigned char key, int x, int y)
  113. {  
  114.     keys[key] = 1;
  115. }
  116.  
  117. void KeyUp(unsigned char key, int x, int y)
  118. {
  119.     keys[key] = 0;
  120. }
  121.  
  122. void PassiveMouse(int _x, int _y)
  123. {
  124.     evt_reg->push_event(new cEvent(_x, 480-_y, MOUSE_EVENT));
  125.     mx = _x;
  126.     my = _y;
  127. }
  128.  
  129. int main(int argc, char **argv)
  130. {
  131.    
  132.     evt_reg = new cEventRegistry();
  133.     ent = new cEntity();
  134.    
  135.     REGISTER_LISTENER(evt_reg, cEventListener, cScriptEventHandler, ent);
  136.    
  137.     cScriptEngine *engine = new cScriptEngine();
  138.     cScriptModule *mod = engine->pGetScriptModule("ent_move");
  139.    
  140.     engine->RegisterScriptable<cEntity>();
  141.     engine->RegisterScriptable<cEvent>();
  142.    
  143.    
  144.     //int x = 12;
  145.     //engine->RegisterGlobalProperty("int x", &x);
  146.    
  147.     /*const char *prop;
  148.     int type;
  149.     int n = engine->getEngine()->GetGlobalPropertyCount();
  150.     for(int a = 0;a < n;a++)
  151.     {
  152.         engine->getEngine()->GetGlobalPropertyByIndex(a, &prop, &type);
  153.         printf("--> %s %s\n", engine->getEngine()->GetTypeDeclaration(type), prop);
  154.     }*/
  155.    
  156.    
  157.     char *script =
  158.     "void ON_EVENT(cEvent &evt)                                             "
  159.     "{                                                                      "
  160.     "   Print(\"Event! \" + evt.getName() + \" : \" + x + \"\\n\");         "
  161.     "   x++;                                                                 "
  162.     "}                                                                      ";
  163.    
  164.    
  165.     //mod->addSection("void func() { Print(er.get_err() + x + \"\\n\"); x++; }");
  166.     //mod->addSection(script);
  167.     mod->addSectionFromFile("test.as");
  168.    
  169.     engine->RegisterGlobalProperty("cEntity me", ent);
  170.    
  171.     mod->buildScript();
  172.    
  173.     cScriptExec *exe = engine->pGetScriptExec("ent_move", "void ON_EVENT(cEvent &evt)");
  174.    
  175.     ((cScriptEventHandler*)ent->getComp("event_handler"))->set_exec(exe);
  176.    
  177.     glutInit(&argc, argv);
  178.    
  179.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  180.     glutInitWindowSize(640, 480);
  181.    
  182.     glutCreateWindow("GLUT Program");
  183.    
  184.     glutDisplayFunc(display);
  185.     glutReshapeFunc(reshape);
  186.     glutIdleFunc(idle);
  187.    
  188.     glutKeyboardFunc(KeyDown);
  189.     glutKeyboardUpFunc(KeyUp);
  190.     glutPassiveMotionFunc(PassiveMouse);
  191.    
  192.     glutMainLoop();
  193.    
  194.    
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement