vrfileman

changeset 2:282da6123fd4

lalalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Feb 2015 12:51:10 +0200
parents 9e3d77dad51b
children 2cbf85690e03
files Makefile src/app.cc src/app.h src/fs.cc src/fs.h src/main.cc src/user.cc src/user.h
diffstat 8 files changed, 365 insertions(+), 28 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Sat Jan 31 20:01:35 2015 +0200
     1.2 +++ b/Makefile	Sun Feb 01 12:51:10 2015 +0200
     1.3 @@ -7,10 +7,9 @@
     1.4  dbg = -g
     1.5  
     1.6  CXXFLAGS = $(warn) $(opt) $(dbg) $(inc)
     1.7 -LDFLAGS = $(libgl)
     1.8 +LDFLAGS = $(libgl) -lvmath
     1.9  
    1.10  
    1.11 -# system-specific stuff
    1.12  ifeq ($(shell uname -s), Darwin)
    1.13  	libgl = -framework OpenGL -framework GLUT -lGLEW
    1.14  else
     2.1 --- a/src/app.cc	Sat Jan 31 20:01:35 2015 +0200
     2.2 +++ b/src/app.cc	Sun Feb 01 12:51:10 2015 +0200
     2.3 @@ -1,6 +1,12 @@
     2.4  #include <assert.h>
     2.5  #include "opengl.h"
     2.6  #include "app.h"
     2.7 +#include "user.h"
     2.8 +
     2.9 +static void draw_grid(int num, float sep);
    2.10 +
    2.11 +static User user;
    2.12 +static float eye_level = 1.6;
    2.13  
    2.14  bool app_init()
    2.15  {
    2.16 @@ -18,6 +24,15 @@
    2.17  {
    2.18  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2.19  
    2.20 +	glMatrixMode(GL_MODELVIEW);
    2.21 +	glLoadIdentity();
    2.22 +
    2.23 +	Matrix4x4 viewmat;
    2.24 +	user.posrot.calc_inv_matrix(&viewmat);
    2.25 +	glLoadTransposeMatrixf(viewmat.m[0]);
    2.26 +	glTranslatef(0, -eye_level, 0);
    2.27 +
    2.28 +	draw_grid(20, 1.0);
    2.29  
    2.30  	swap_buffers();
    2.31  	assert(glGetError() == GL_NO_ERROR);
    2.32 @@ -49,3 +64,41 @@
    2.33  void app_mouse_motion(int x, int y)
    2.34  {
    2.35  }
    2.36 +
    2.37 +void app_sball_motion(float x, float y, float z)
    2.38 +{
    2.39 +}
    2.40 +
    2.41 +void app_sball_rotate(float x, float y, float z)
    2.42 +{
    2.43 +}
    2.44 +
    2.45 +void app_sball_button(int bn, bool pressed)
    2.46 +{
    2.47 +}
    2.48 +
    2.49 +static void draw_grid(int num, float sep)
    2.50 +{
    2.51 +	int hnum = num / 2;
    2.52 +	float max_dist = hnum * sep;
    2.53 +
    2.54 +	glBegin(GL_LINES);
    2.55 +	glVertex3f(0, 0, -max_dist);
    2.56 +	glVertex3f(0, 0, max_dist);
    2.57 +	glVertex3f(-max_dist, 0, 0);
    2.58 +	glVertex3f(max_dist, 0, 0);
    2.59 +
    2.60 +	for(int i=1; i<hnum; i++) {
    2.61 +		float x = i * sep;
    2.62 +
    2.63 +		glVertex3f(-x, 0, -max_dist);
    2.64 +		glVertex3f(-x, 0, max_dist);
    2.65 +		glVertex3f(x, 0, -max_dist);
    2.66 +		glVertex3f(x, 0, max_dist);
    2.67 +		glVertex3f(-max_dist, 0, -x);
    2.68 +		glVertex3f(max_dist, 0, -x);
    2.69 +		glVertex3f(-max_dist, 0, x);
    2.70 +		glVertex3f(max_dist, 0, x);
    2.71 +	}
    2.72 +	glEnd();
    2.73 +}
     3.1 --- a/src/app.h	Sat Jan 31 20:01:35 2015 +0200
     3.2 +++ b/src/app.h	Sun Feb 01 12:51:10 2015 +0200
     3.3 @@ -9,6 +9,9 @@
     3.4  void app_keyboard(int key, bool pressed, int x, int y);
     3.5  void app_mouse_button(int bn, bool pressed, int x, int y);
     3.6  void app_mouse_motion(int x, int y);
     3.7 +void app_sball_motion(float x, float y, float z);
     3.8 +void app_sball_rotate(float x, float y, float z);
     3.9 +void app_sball_button(int bn, bool pressed);
    3.10  
    3.11  /* provided by the frontend */
    3.12  enum {
     4.1 --- a/src/fs.cc	Sat Jan 31 20:01:35 2015 +0200
     4.2 +++ b/src/fs.cc	Sun Feb 01 12:51:10 2015 +0200
     4.3 @@ -2,13 +2,17 @@
     4.4  #include <string.h>
     4.5  #include <ctype.h>
     4.6  #include <errno.h>
     4.7 +#include <algorithm>
     4.8  #include <alloca.h>
     4.9  #include <unistd.h>
    4.10  #include <dirent.h>
    4.11 +#include <sys/stat.h>
    4.12  #include "fs.h"
    4.13  
    4.14 +static bool childcmpless(const FSNode *aptr, const FSNode *bptr);
    4.15  static char *clean_path(char *path);
    4.16  static char *filename(char *path);
    4.17 +static FSNode::Type st_mode_to_type(mode_t mode);
    4.18  
    4.19  FSNode::FSNode()
    4.20  {
    4.21 @@ -25,7 +29,9 @@
    4.22  	path = name = 0;
    4.23  	parent = 0;
    4.24  	expanded = false;
    4.25 +	sorted = true;
    4.26  	uid = gid = mode = 0;
    4.27 +	type = UNKNOWN;
    4.28  }
    4.29  
    4.30  void FSNode::destroy()
    4.31 @@ -44,6 +50,32 @@
    4.32  	destroy();
    4.33  }
    4.34  
    4.35 +void FSNode::set_type(Type type)
    4.36 +{
    4.37 +	this->type = type;
    4.38 +}
    4.39 +
    4.40 +FSNode::Type FSNode::get_type() const
    4.41 +{
    4.42 +	return type;
    4.43 +}
    4.44 +
    4.45 +bool FSNode::is_file() const
    4.46 +{
    4.47 +	return type != DIRECTORY;
    4.48 +}
    4.49 +
    4.50 +bool FSNode::is_directory() const
    4.51 +{
    4.52 +	return type == DIRECTORY;
    4.53 +}
    4.54 +
    4.55 +void FSNode::sort_children()
    4.56 +{
    4.57 +	std::sort(children.begin(), children.end(), childcmpless);
    4.58 +	sorted = true;
    4.59 +}
    4.60 +
    4.61  void FSNode::set_path(const char *path)
    4.62  {
    4.63  	delete [] this->path;
    4.64 @@ -77,25 +109,146 @@
    4.65  	}
    4.66  }
    4.67  
    4.68 -#if 0
    4.69 -FSDir *create_fsdir(const char *path)
    4.70 +const char *FSNode::get_path() const
    4.71  {
    4.72 -	char *pathbuf;
    4.73 +	return path;
    4.74 +}
    4.75  
    4.76 -	FSDir *node = new FSDir;
    4.77 -	node->name = std::string(filename(path));
    4.78 +const char *FSNode::get_name() const
    4.79 +{
    4.80 +	return name;
    4.81 +}
    4.82 +
    4.83 +bool FSNode::add_child(FSNode *node)
    4.84 +{
    4.85 +	if(node->parent == this) {
    4.86 +		return true;
    4.87 +	}
    4.88 +
    4.89 +	if(node->parent) {
    4.90 +		node->parent->remove_child(node);
    4.91 +	}
    4.92 +	node->parent = this;
    4.93 +
    4.94 +	try {
    4.95 +		children.push_back(node);
    4.96 +	}
    4.97 +	catch(...) {
    4.98 +		return false;
    4.99 +	}
   4.100 +
   4.101 +	sorted = false;
   4.102 +	return true;
   4.103 +}
   4.104 +
   4.105 +bool FSNode::remove_child(FSNode *node)
   4.106 +{
   4.107 +	int cidx = find_child(node);
   4.108 +	if(cidx == -1) {
   4.109 +		return false;
   4.110 +	}
   4.111 +	children.erase(children.begin() + cidx);
   4.112 +
   4.113 +	if(node->parent != this) {
   4.114 +		fprintf(stderr, "FSNode::remove_child(): target node doesn't have this node as parent\n");
   4.115 +		// let's not touch it if this happens...
   4.116 +	} else {
   4.117 +		node->parent = 0;
   4.118 +	}
   4.119 +	return true;
   4.120 +}
   4.121 +
   4.122 +int FSNode::find_child(FSNode *node) const
   4.123 +{
   4.124 +	for(size_t i=0; i<children.size(); i++) {
   4.125 +		if(children[i] == node) {
   4.126 +			return i;
   4.127 +		}
   4.128 +	}
   4.129 +	return -1;
   4.130 +}
   4.131 +
   4.132 +int FSNode::find_child(const char *name) const
   4.133 +{
   4.134 +	FSNode key;
   4.135 +	key.name = (char*)name;
   4.136 +
   4.137 +	if(!sorted) {
   4.138 +		((FSNode*)this)->sort_children();
   4.139 +	}
   4.140 +
   4.141 +	std::vector<FSNode*>::const_iterator it;
   4.142 +	it = std::lower_bound(children.begin(), children.end(), &key, childcmpless);
   4.143 +	if(it == children.end() || strcmp((*it)->name, name) != 0) {
   4.144 +		return -1;
   4.145 +	}
   4.146 +	return std::distance(children.begin(), it);
   4.147 +}
   4.148 +
   4.149 +FSNode *FSNode::get_parent()
   4.150 +{
   4.151 +	return parent;
   4.152 +}
   4.153 +
   4.154 +const FSNode *FSNode::get_parent() const
   4.155 +{
   4.156 +	return parent;
   4.157 +}
   4.158 +
   4.159 +int FSNode::get_child_count() const
   4.160 +{
   4.161 +	return (int)children.size();
   4.162 +}
   4.163 +
   4.164 +FSNode *FSNode::get_child(int n)
   4.165 +{
   4.166 +	if(!sorted) {
   4.167 +		sort_children();
   4.168 +	}
   4.169 +	return children[n];
   4.170 +}
   4.171 +
   4.172 +const FSNode *FSNode::get_child(int n) const
   4.173 +{
   4.174 +	if(!sorted) {
   4.175 +		((FSNode*)this)->sort_children();
   4.176 +	}
   4.177 +	return children[n];
   4.178 +}
   4.179 +
   4.180 +bool FSNode::expand()
   4.181 +{
   4.182 +	expanded = true;
   4.183 +	return true;
   4.184 +}
   4.185 +
   4.186 +bool FSNode::is_expanded() const
   4.187 +{
   4.188 +	return expanded;
   4.189 +}
   4.190 +
   4.191 +
   4.192 +// ---- FSDir ----
   4.193 +FSDir::FSDir()
   4.194 +{
   4.195 +	type = DIRECTORY;
   4.196 +}
   4.197 +
   4.198 +bool FSDir::expand()
   4.199 +{
   4.200 +	if(expanded) return true;
   4.201  
   4.202  	DIR *dir = opendir(path);
   4.203  	if(!dir) {
   4.204 -		fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno));
   4.205 -		return 0;
   4.206 +		fprintf(stderr, "FSDir::expand() failed to open dir: %s: %s\n", path, strerror(errno));
   4.207 +		return false;
   4.208  	}
   4.209  
   4.210 -	pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2);
   4.211 +	char *pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2);
   4.212  
   4.213  	struct dirent *ent;
   4.214  	while((ent = readdir(dir))) {
   4.215 -		sprintf(pathbuf, "%s/%s", path, ent->d_ent);
   4.216 +		sprintf(pathbuf, "%s/%s", path, ent->d_name);
   4.217  
   4.218  		struct stat st;
   4.219  		if(stat(pathbuf, &st) == -1) {
   4.220 @@ -103,18 +256,49 @@
   4.221  			continue;
   4.222  		}
   4.223  
   4.224 -		if(S_ISDIR(st.st_type)) {
   4.225 -			FSDir *subdir = new FSDir;
   4.226 -			subdir->name = std::string(ent->d_ent);
   4.227 -			add_subdir(node, subdir);
   4.228 +		FSNode *child;
   4.229 +		if(S_ISDIR(st.st_mode)) {
   4.230 +			child = new FSDir;
   4.231  		} else {
   4.232 -			FSFile *file =  new FSFile;
   4.233 -			file->name = std::string(ent->d_ent);
   4.234 -			file->parent = node;
   4.235 +			FSFile *file = new FSFile;
   4.236 +			file->set_size(st.st_size);
   4.237 +			file->set_type(st_mode_to_type(st.st_mode));
   4.238 +			child = file;
   4.239  		}
   4.240 +		add_child(child);
   4.241 +		child->set_name(ent->d_name);
   4.242  	}
   4.243 +
   4.244 +	closedir(dir);
   4.245 +
   4.246 +	expanded = true;
   4.247 +	return true;
   4.248  }
   4.249 -#endif
   4.250 +
   4.251 +// ---- FSFile ----
   4.252 +
   4.253 +FSFile::FSFile()
   4.254 +{
   4.255 +	type = UNKNOWN;
   4.256 +	size = 0;
   4.257 +}
   4.258 +
   4.259 +void FSFile::set_size(unsigned long s)
   4.260 +{
   4.261 +	size = s;
   4.262 +}
   4.263 +
   4.264 +unsigned long FSFile::get_size() const
   4.265 +{
   4.266 +	return size;
   4.267 +}
   4.268 +
   4.269 +// ---- static helpers ----
   4.270 +
   4.271 +static bool childcmpless(const FSNode *aptr, const FSNode *bptr)
   4.272 +{
   4.273 +	return strcmp(aptr->get_name(), bptr->get_name()) < 0;
   4.274 +}
   4.275  
   4.276  static char *clean_path(char *path)
   4.277  {
   4.278 @@ -144,3 +328,25 @@
   4.279  	}
   4.280  	return path;
   4.281  }
   4.282 +
   4.283 +static FSNode::Type st_mode_to_type(mode_t mode)
   4.284 +{
   4.285 +	switch(mode & S_IFMT) {
   4.286 +	case S_IFDIR:
   4.287 +		return FSNode::DIRECTORY;
   4.288 +	case S_IFREG:
   4.289 +		return FSNode::REGFILE;
   4.290 +	case S_IFLNK:
   4.291 +		return FSNode::LINK;
   4.292 +	case S_IFBLK:
   4.293 +	case S_IFCHR:
   4.294 +		return FSNode::DEVICE;
   4.295 +	case S_IFSOCK:
   4.296 +		return FSNode::SOCKET;
   4.297 +	case S_IFIFO:
   4.298 +		return FSNode::FIFO;
   4.299 +	default:
   4.300 +		break;
   4.301 +	}
   4.302 +	return FSNode::UNKNOWN;
   4.303 +}
     5.1 --- a/src/fs.h	Sat Jan 31 20:01:35 2015 +0200
     5.2 +++ b/src/fs.h	Sun Feb 01 12:51:10 2015 +0200
     5.3 @@ -4,26 +4,37 @@
     5.4  #include <vector>
     5.5  
     5.6  class FSNode {
     5.7 +public:
     5.8 +	enum Type { UNKNOWN, DIRECTORY, REGFILE, LINK, DEVICE, SOCKET, FIFO };
     5.9 +
    5.10  protected:
    5.11  	char *path, *name;
    5.12  	FSNode *parent;
    5.13  
    5.14  	std::vector<FSNode*> children;
    5.15 -	bool expanded;
    5.16 +	bool expanded, sorted;
    5.17  
    5.18 +	Type type;
    5.19  	unsigned int uid, gid, mode;
    5.20  
    5.21  private:
    5.22  	FSNode(const FSNode&);
    5.23  	FSNode &operator =(const FSNode&);
    5.24  
    5.25 +	void sort_children();
    5.26 +
    5.27  public:
    5.28  	FSNode();
    5.29  	virtual ~FSNode();
    5.30  
    5.31 -	void init();
    5.32 -	void destroy();
    5.33 -	void destroy_tree();
    5.34 +	virtual void init();
    5.35 +	virtual void destroy();
    5.36 +	virtual void destroy_tree();
    5.37 +
    5.38 +	virtual void set_type(Type type);
    5.39 +	virtual Type get_type() const;
    5.40 +	virtual bool is_file() const;
    5.41 +	virtual bool is_directory() const;
    5.42  
    5.43  	virtual void set_path(const char *path);
    5.44  	virtual void set_name(const char *name);
    5.45 @@ -31,7 +42,11 @@
    5.46  	virtual const char *get_path() const;
    5.47  	virtual const char *get_name() const;
    5.48  
    5.49 -	virtual void add_child(FSNode *node);
    5.50 +	virtual bool add_child(FSNode *node);
    5.51 +	virtual bool remove_child(FSNode *node);
    5.52 +
    5.53 +	virtual int find_child(FSNode *node) const;
    5.54 +	virtual int find_child(const char *name) const;
    5.55  
    5.56  	virtual FSNode *get_parent();
    5.57  	virtual const FSNode *get_parent() const;
    5.58 @@ -40,7 +55,7 @@
    5.59  	virtual FSNode *get_child(int n);
    5.60  	virtual const FSNode *get_child(int n) const;
    5.61  
    5.62 -	virtual void expand();
    5.63 +	virtual bool expand();
    5.64  	virtual bool is_expanded() const;
    5.65  };
    5.66  
    5.67 @@ -48,18 +63,19 @@
    5.68  public:
    5.69  	FSDir();
    5.70  
    5.71 -	virtual void expand();
    5.72 +	virtual bool expand();
    5.73  };
    5.74  
    5.75  class FSFile : public FSNode {
    5.76  protected:
    5.77  	unsigned long size;
    5.78 +	enum Type type;
    5.79  
    5.80  public:
    5.81  	FSFile();
    5.82  
    5.83 -	void set_size(unsigned long s);
    5.84 -	unsigned long get_size() const;
    5.85 +	virtual void set_size(unsigned long s);
    5.86 +	virtual unsigned long get_size() const;
    5.87  };
    5.88  
    5.89  #endif	// FS_H_
     6.1 --- a/src/main.cc	Sat Jan 31 20:01:35 2015 +0200
     6.2 +++ b/src/main.cc	Sun Feb 01 12:51:10 2015 +0200
     6.3 @@ -15,6 +15,9 @@
     6.4  static void key_up(unsigned char key, int x, int y);
     6.5  static void mouse(int bn, int state, int x, int y);
     6.6  static void motion(int x, int y);
     6.7 +static void sball_motion(int x, int y, int z);
     6.8 +static void sball_rotate(int x, int y, int z);
     6.9 +static void sball_button(int bn, int state);
    6.10  
    6.11  static int win_width, win_height;
    6.12  static unsigned int mod;
    6.13 @@ -34,6 +37,9 @@
    6.14  	glutMouseFunc(mouse);
    6.15  	glutMotionFunc(motion);
    6.16  	glutPassiveMotionFunc(motion);
    6.17 +	glutSpaceballMotionFunc(sball_motion);
    6.18 +	glutSpaceballRotateFunc(sball_rotate);
    6.19 +	glutSpaceballButtonFunc(sball_button);
    6.20  
    6.21  	if(app_init() == -1) {
    6.22  		return 1;
    6.23 @@ -104,3 +110,18 @@
    6.24  {
    6.25  	app_mouse_motion(x, y);
    6.26  }
    6.27 +
    6.28 +static void sball_motion(int x, int y, int z)
    6.29 +{
    6.30 +	app_sball_motion(x / 1000.0f, y / 1000.0f, z / 1000.0f);
    6.31 +}
    6.32 +
    6.33 +static void sball_rotate(int x, int y, int z)
    6.34 +{
    6.35 +	app_sball_rotate(x / 1800.0f, y / 1800.0f, z / 1800.0f);
    6.36 +}
    6.37 +
    6.38 +static void sball_button(int bn, int state)
    6.39 +{
    6.40 +	app_sball_button(bn, state == GLUT_DOWN);
    6.41 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/user.cc	Sun Feb 01 12:51:10 2015 +0200
     7.3 @@ -0,0 +1,17 @@
     7.4 +#include "user.h"
     7.5 +
     7.6 +void PosRot::move(float dfwd, float dright, float dup)
     7.7 +{
     7.8 +}
     7.9 +
    7.10 +void PosRot::rotate(float dhoriz, float dvert)
    7.11 +{
    7.12 +}
    7.13 +
    7.14 +void PosRot::calc_matrix(Matrix4x4 *res) const
    7.15 +{
    7.16 +}
    7.17 +
    7.18 +void PosRot::calc_inv_matrix(Matrix4x4 *res) const
    7.19 +{
    7.20 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/user.h	Sun Feb 01 12:51:10 2015 +0200
     8.3 @@ -0,0 +1,22 @@
     8.4 +#ifndef USER_H_
     8.5 +#define USER_H_
     8.6 +
     8.7 +#include <vmath/vmath.h>
     8.8 +
     8.9 +struct PosRot {
    8.10 +	Vector3 pos;
    8.11 +	Quaternion rot;
    8.12 +
    8.13 +	void move(float dfwd, float dright, float dup);
    8.14 +	void rotate(float dhoriz, float dvert);
    8.15 +
    8.16 +	void calc_matrix(Matrix4x4 *res) const;
    8.17 +	void calc_inv_matrix(Matrix4x4 *res) const;
    8.18 +};
    8.19 +
    8.20 +class User {
    8.21 +public:
    8.22 +	PosRot posrot;
    8.23 +};
    8.24 +
    8.25 +#endif	// USER_H_