changeset 140:861271089c43

add Controller
author gongo@charles.cr.ie.u-ryukyu.ac.jp
date Fri, 28 Nov 2008 15:24:55 +0900
parents c948f4ebde62
children fc314f28b66e
files TaskManager/Test/test_render/Button.cpp TaskManager/Test/test_render/Button.h TaskManager/Test/test_render/Joystick.cpp TaskManager/Test/test_render/Joystick.h TaskManager/Test/test_render/Keyboard.cpp TaskManager/Test/test_render/Keyboard.h TaskManager/Test/test_render/Makefile.def TaskManager/Test/test_render/Pad.h TaskManager/Test/test_render/SceneGraph.cpp TaskManager/Test/test_render/SceneGraph.h TaskManager/Test/test_render/node.cpp TaskManager/Test/test_render/pad.h TaskManager/Test/test_render/viewer.cpp
diffstat 13 files changed, 402 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Button.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,26 @@
+#include "Button.h"
+
+void
+Button::push_work(void)
+{
+#if 0
+    if (hold) {
+	push = 0;
+    } else {
+	push = 1;
+    }
+#else
+    push = (!hold)*1;
+#endif
+
+    hold = 1;
+    release = 0;
+}
+
+void
+Button::release_work(void)
+{
+    push = 0;
+    hold = 0;
+    release = 1;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Button.h	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,16 @@
+#ifndef INCLUDED_BUTTON
+#define INCLUDED_BUTTON
+
+class Button {
+public:
+    int push;
+    int hold;
+    int release;
+
+    Button(void) : push(0), hold(0), release(0) {}
+
+    void push_work(void);
+    void release_work(void);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Joystick.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,134 @@
+#include <SDL.h>
+#include "Joystick.h"
+
+static const int CROSS = 0;
+static const int CIRCLE = 1;
+static const int SQUARE = 2;
+static const int TRIANGLE = 3;
+static const int L1 = 4;
+static const int R1 = 5;
+static const int L2 = 6;
+static const int R2 = 7;
+static const int START = 8;
+static const int SELECT = 9;
+static const int L3 = 10;
+static const int R3 = 11;
+static const int UP = 12;
+static const int DOWN = 13;
+static const int RIGHT = 14;
+static const int LEFT = 15;
+static const int ESCAPE = 16;
+static const int SPACE = 17;
+
+Joystick::Joystick(SDL_Joystick *j)
+{
+    joy = j;
+}
+
+void
+Joystick::check(void)
+{
+    SDL_JoystickUpdate();
+
+    if (SDL_JoystickGetButton(joy,CROSS)==SDL_PRESSED) {
+	    cross.push_work();
+    } else {
+	cross.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,CIRCLE)==SDL_PRESSED) {
+	circle.push_work();
+    } else {
+	circle.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,SQUARE)==SDL_PRESSED) {
+	square.push_work();
+    } else {
+	square.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,TRIANGLE)==SDL_PRESSED) {
+	triangle.push_work();
+    } else {
+	triangle.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,L1)==SDL_PRESSED) {
+	l1.push_work();
+    } else {
+	l1.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,R1)==SDL_PRESSED) {
+	r1.push_work();
+    } else {
+	r1.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,L2)==SDL_PRESSED) {
+	l2.push_work();
+    } else {
+	l2.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,R2)==SDL_PRESSED) {
+	r2.push_work();
+    } else {
+	r2.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,START)==SDL_PRESSED) {
+	start.push_work();
+    } else {
+	start.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,SELECT)==SDL_PRESSED) {
+	select.push_work();
+    } else {
+	select.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,L3)==SDL_PRESSED) {
+	l3.push_work();
+    } else {
+	l3.release_work();
+    }
+
+    if (SDL_JoystickGetButton(joy,R3)==SDL_PRESSED) {
+	r3.push_work();
+    } else {
+	r3.release_work();
+    }
+
+    axis = SDL_JoystickGetAxis(joy,0);
+    if (axis >= 3200) {
+	left.release_work();
+	right.push_work();
+    } else if (axis <= -3200) {
+	right.release_work();
+	left.push_work();
+    } else {
+	left.release_work();
+	right.release_work();
+    }
+
+    axis = SDL_JoystickGetAxis(joy,1);
+    if (axis>=3200) {
+	up.release_work();
+	down.push_work();
+    } else if (axis<=-3200) {
+	down.release_work();
+	up.push_work();
+    } else {
+	up.release_work();
+	down.release_work();
+    }
+}
+
+void
+Joystick::close(void)
+{
+    SDL_JoystickClose(joy);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Joystick.h	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,19 @@
+#ifndef INCLUDED_JOYSTICK
+#define INCLUDED_JOYSTICK
+
+#ifndef INCLUDED_PAD
+#  include "Pad.h"
+#endif
+
+class Joystick : public Pad {
+public:
+    SDL_Joystick *joy;
+    Sint16 axis;
+
+    Joystick(SDL_Joystick *j);
+
+    void check(void);
+    void close(void);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Keyboard.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,68 @@
+#include <SDL.h>
+#include "Keyboard.h"
+
+void
+Keyboard::check(void)
+{
+    Uint8 *keys = SDL_GetKeyState(NULL);
+
+    if (keys[SDLK_UP] == SDL_PRESSED) {
+	up.push_work();
+    } else {
+	up.release_work();
+    }
+
+    if (keys[SDLK_DOWN] == SDL_PRESSED) {
+	down.push_work();
+    } else {
+	down.release_work();
+    }
+
+    if (keys[SDLK_RIGHT] == SDL_PRESSED) {
+	right.push_work();
+    } else {
+	right.release_work();
+    }
+
+    if (keys[SDLK_LEFT] == SDL_PRESSED) {
+	left.push_work();
+    } else {
+	left.release_work();
+    }
+
+    if (keys[SDLK_RETURN] == SDL_PRESSED) {
+	start.push_work();
+    } else {
+	start.release_work();
+    }
+
+    if (keys[SDLK_ESCAPE] == SDL_PRESSED) {
+	select.push_work();
+    } else {
+	select.release_work();
+    }
+
+    if (keys[SDLK_a] == SDL_PRESSED) {
+	square.push_work();
+    } else {
+	square.release_work();
+    }
+
+    if (keys[SDLK_s] == SDL_PRESSED) {
+	triangle.push_work();
+    } else {
+	triangle.release_work();
+    }
+
+    if (keys[SDLK_z] == SDL_PRESSED) {
+	cross.push_work();
+    } else {
+	cross.release_work();
+    }
+
+    if (keys[SDLK_x] == SDL_PRESSED) {
+	circle.push_work();
+    } else {
+	circle.release_work();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Keyboard.h	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,13 @@
+#ifndef INCLUDED_KEYBOARD
+#define INCLUDED_KEYBOARD
+
+#ifndef INCLUDED_PAD
+#  include "Pad.h"
+#endif
+
+class Keyboard : public Pad {
+public:
+    void check(void);
+};
+
+#endif
--- a/TaskManager/Test/test_render/Makefile.def	Fri Nov 28 13:51:54 2008 +0900
+++ b/TaskManager/Test/test_render/Makefile.def	Fri Nov 28 15:24:55 2008 +0900
@@ -11,7 +11,7 @@
 #CERIUM = ../../..
 
 CC      = g++
-CFLAGS  = -O9 -g -Wall# -DDEBUG
+CFLAGS  = -O0 -g -Wall# -DDEBUG
 
 INCLUDE = -I$(CERIUM)/include/TaskManager -I.
 LIBS = -L$(CERIUM)/TaskManager
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/Pad.h	Fri Nov 28 15:24:55 2008 +0900
@@ -0,0 +1,35 @@
+#ifndef INCLUDED_PAD
+#define INCLUDED_PAD
+
+#ifndef INCLUDED_BUTTON
+#  include "Button.h"
+#endif
+
+class Pad {
+public:
+    Button count;
+    Button cross;
+    Button circle;
+    Button square;
+    Button triangle;
+    Button l1;
+    Button r1;
+    Button l2;
+    Button r2;
+    Button start;
+    Button select;
+    Button l3;
+    Button r3;
+    Button up;
+    Button down;
+    Button right;
+    Button left;
+    Button escape;
+    Button space;
+
+    virtual ~Pad(void) {}
+    virtual void check(void) = 0;
+    virtual void close(void) {}
+};
+
+#endif
--- a/TaskManager/Test/test_render/SceneGraph.cpp	Fri Nov 28 13:51:54 2008 +0900
+++ b/TaskManager/Test/test_render/SceneGraph.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -18,8 +18,19 @@
 no_collision(SceneGraphPtr self, int screen_w, int screen_h,
 	     SceneGraphPtr tree) {}
 
+SceneGraphPtr scene_graph = NULL;
+SceneGraphPtr scene_graph_viewer = NULL;
+
+
 SceneGraph::SceneGraph(void)
 {
+    init();
+}
+
+
+void
+SceneGraph::init(void)
+{
     next = NULL;
     last = NULL;
 
@@ -39,23 +50,10 @@
     collision = no_collision;
 }
 
-
 /* construct polygon from xmlNode.  */
 SceneGraph::SceneGraph(xmlNodePtr surface)
 {
-#if 1
-    next = NULL;
-    last = NULL;
-    parent = NULL;
-    brother = NULL;
-    children = NULL;
-    lastChild = NULL;
-    move = no_move;
-    collision = no_collision;
-#else
-    // こうしたいんだけどなー
-    this->SceneGraph();
-#endif
+    init();
 
     size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size"));
     name = (char *)xmlGetProp(surface,(xmlChar *)"name");
@@ -66,15 +64,19 @@
     get_data(surface->children);
 }
 
+SceneGraph::~SceneGraph(void)
+{
+    delete [] data;
+}
 
 /* XMLファイルからポリゴンを作成  */
-SceneGraph*
+void
 SceneGraph::createFromXMLfile(char *xmlfile)
 {
     xmlDocPtr doc;
     xmlNodePtr cur;
-    SceneGraph *root = NULL, *tmp, *parent;
-
+    SceneGraphPtr root = NULL, tmp, parent;
+    
     /* パース DOM生成 */
     doc = xmlParseFile(xmlfile);
     cur = xmlDocGetRootElement(doc);
@@ -94,6 +96,7 @@
 	if ( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) {
 	    /* このsurfaceがroot  */
 	    root = tmp;
+	    scene_graph = tmp;
 	} else {
 	    /* 親はこのsurfaceより前に定義されているものとする (していい?)  */
 	    //  ここで parent_name を用いるのは間違っていて、
@@ -106,12 +109,15 @@
 	    } else {
 		parent->addChild(tmp);
 	    }
-	    root->add_next(tmp);
+
+	    scene_graph->add_next(tmp);
 	}
     }
   
     xmlFreeDoc(doc);
-    return root;
+
+    //return root;
+    scene_graph_viewer = root;
 }
 
 /* 子供を追加  */
@@ -313,18 +319,16 @@
 void
 SceneGraph::delete_data(void)
 {
-    SceneGraph *n,*m;
+    SceneGraphPtr n = this->next, m;
 
-    n = this;
-    delete [] n->data;
+    //n = this;
+    //delete [] n->data;
 
-    if (next)
-    {
-	for(n = this->next; n; n=m)
-	{
+    if (next) {
+	while (n) {
 	    m = n->next;
-	    delete [] n->data;
 	    delete n;
+	    n = m;
 	}
     }
 }
@@ -405,25 +409,6 @@
 SceneGraph::clone(void) {
     SceneGraphPtr p = new SceneGraph;
     memcpy(p, this, sizeof(SceneGraph));
-
-
-    // どっかで関数にまとめるか
-    p->next = NULL;
-    p->last = NULL;
-
-    p->parent = NULL;
-    p->brother = NULL;
-    p->children = NULL;
-    p->lastChild = NULL;
-    p->move = no_move;
-    p->collision = no_collision;
-
-    p->stack_xyz[0] = 0.0f;
-    p->stack_xyz[2] = 0.0f;
-    p->stack_xyz[1] = 0.0f;
-    p->stack_angle[0] = 0.0f;
-    p->stack_angle[1] = 0.0f;
-    p->stack_angle[2] = 0.0f;
-    
+    p->init();
     return p;
 }
--- a/TaskManager/Test/test_render/SceneGraph.h	Fri Nov 28 13:51:54 2008 +0900
+++ b/TaskManager/Test/test_render/SceneGraph.h	Fri Nov 28 15:24:55 2008 +0900
@@ -5,6 +5,10 @@
 #  include "polygon.h"
 #endif
 
+#ifndef INCLUDED_PAD
+#  include "Pad.h"
+#endif
+
 class SceneGraph;
 
 typedef void (*move_func)(SceneGraph* node, int screen_w, int screen_h);
@@ -16,6 +20,7 @@
 public:
     SceneGraph(void);
     SceneGraph(xmlNodePtr surface);
+    ~SceneGraph(void);
 
     // Node がもつ状態変数(というべきか否か
     // xyz,angle ぐらいあればおk?
@@ -32,10 +37,13 @@
     SceneGraph *children;
     SceneGraph *lastChild;
 
+    Pad *controller;
+
     // 関数ポインタ
     move_func move;
     collision_func collision;
 
+    void init(void);
     void move_execute(int screen_w, int screen_h);
     void collision_check(int screen_w, int screen_h, SceneGraph *tree);
     void all_execute(int screen_w, int screen_h);
@@ -49,7 +57,7 @@
 			    move_func new_move, collision_func new_collision);
 
 
-    static SceneGraph* createFromXMLfile(char *);
+    static void createFromXMLfile(char *);
 
     void tree_check(void);
     void print_member(void);
@@ -59,4 +67,8 @@
 
 #endif
 
+// オリジナル (Linked List)
 extern SceneGraphPtr scene_graph;
+
+// 描画用 (同じオブジェクトが複数ある) Tree
+extern SceneGraphPtr scene_graph_view;
--- a/TaskManager/Test/test_render/node.cpp	Fri Nov 28 13:51:54 2008 +0900
+++ b/TaskManager/Test/test_render/node.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -88,6 +88,7 @@
     node->xyz[1] += node->stack_xyz[1];
     if ((int)node->xyz[1] > screen_h || (int)node->xyz[1] < 0) {
 
+	// 実は微妙に意味が無い
 	srandom(random());
 
 	SceneGraphPtr p = node->clone();
@@ -108,9 +109,8 @@
 node_init(void)
 {
 #if 0
-    //scene_graph->set_move_collision(Earth, earth_move, earth_collision);
-    //scene_graph->set_move_collision(Moon, moon_move, moon_collision);
-    //scene_graph->set_move_collision(Plain, cube_move, cube_collision);
+    scene_graph->set_move_collision(Earth, earth_move, earth_collision);
+    scene_graph->set_move_collision(Moon, moon_move, moon_collision);
 #else
     scene_graph->set_move_collision(Cube, cube_move, cube_collision);
     Cube->stack_xyz[0] = 2.0f;
--- a/TaskManager/Test/test_render/pad.h	Fri Nov 28 13:51:54 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-const int CROSS = 0;
-const int CIRCLE = 1;
-const int SQUARE = 2;
-const int TRIANGLE = 3;
-const int L1 = 4;
-const int R1 = 5;
-const int L2 = 6;
-const int R2 = 7;
-const int START = 8;
-const int SELECT = 9;
-const int L3 = 10;
-const int R3 = 11;
-const int UP = 12;
-const int DOWN = 13;
-const int RIGHT = 14;
-const int LEFT = 15;
-
-int pad(int button);
--- a/TaskManager/Test/test_render/viewer.cpp	Fri Nov 28 13:51:54 2008 +0900
+++ b/TaskManager/Test/test_render/viewer.cpp	Fri Nov 28 15:24:55 2008 +0900
@@ -1,3 +1,4 @@
+#include <SDL.h>
 #include "viewer.h"
 #include "viewer_types.h"
 #include "SceneGraph.h"
@@ -6,6 +7,8 @@
 #include "Func.h"
 #include "error.h"
 #include "TaskManager.h"
+#include "Keyboard.h"
+#include "Joystick.h"
 
 extern void post2runLoop(void *);
 extern void post2runDraw(void *);
@@ -15,7 +18,8 @@
 int this_time;
 int frames;
 
-SceneGraph *scene_graph;
+extern SceneGraphPtr scene_graph;
+extern SceneGraphPtr scene_graph_viewer;
 
 /* Data Pack */
 SceneGraphPack *sgpack;
@@ -27,6 +31,30 @@
 
 void *__texture;
 
+
+/**
+ * Joystick があればそれを使い、
+ * 無ければキーボードを返す
+ */
+static Pad*
+create_controller(void)
+{
+    if (SDL_NumJoysticks()) {
+	SDL_Joystick *joy = SDL_JoystickOpen(0);
+	if (!joy) {
+	    fprintf(stderr, "%s: failed to open joystick", __FUNCTION__);
+	    return new Keyboard;
+	} else {
+	    printf("Use Joystick\n");
+	    return new Joystick(joy);
+	}
+    } else {
+	printf("Use Keyboard\n");
+	return new Keyboard;
+    }
+}
+
+
 Viewer::Viewer(int b, int w, int h, int _num)
 {
     bpp = b;
@@ -87,8 +115,16 @@
     this_time  = 0;
     frames     = 0;
 
-    scene_graph = SceneGraph::createFromXMLfile(xml);
+    //scene_graph = SceneGraph::createFromXMLfile(xml);
+    /**
+     * これを実行すると、
+     * scene_graph にオリジナルの SceneGraph Object のリストが、
+     * scene_graph_viewer に描画用の SceneGraph が生成される
+     */
+    SceneGraph::createFromXMLfile(xml);
     //polygon->viewer  = this;
+
+    scene_graph->controller = create_controller();
 	
     node_init();
 
@@ -210,7 +246,8 @@
     task_next->wait_for(task_update_sgp);
     task_update_sgp->spawn();
 #else
-    scene_graph->all_execute(width, height);
+    scene_graph->controller->check();
+    scene_graph_viewer->all_execute(width, height);
 #endif
 
 #if 0
@@ -219,7 +256,7 @@
     task_create_pp->add_param((uint32)ppack);
 #else
     task_create_pp = manager->create_task(TASK_CREATE_PP2);
-    task_create_pp->add_param((uint32)scene_graph);
+    task_create_pp->add_param((uint32)scene_graph_viewer);
     task_create_pp->add_param((uint32)ppack);
 #endif
     task_next->wait_for(task_create_pp);
@@ -331,6 +368,7 @@
     }
 
     scene_graph->delete_data();
+    scene_graph->controller->close();
     delete scene_graph;
 
     free(__texture);