Prechádzať zdrojové kódy

Added event example in simple.c. Fixed some valgrind/memory leak issues.

Fredrik Hultin 8 rokov pred
rodič
commit
85cbbd9341
5 zmenil súbory, kde vykonal 37 pridanie a 3 odobranie
  1. 27 2
      examples/simple/simple.c
  2. 1 0
      src/drv_dummy/dummy.c
  3. 5 1
      src/openhmd.c
  4. 1 0
      src/queue.c
  5. 3 0
      tests/unittests/queue.c

+ 27 - 2
examples/simple/simple.c

@@ -17,12 +17,23 @@ void print_infof(ohmd_device* hmd, const char* name, int len, ohmd_float_value v
 {
 	float f[len];
 	ohmd_device_getf(hmd, val, f);
-	printf("%-20s", name);
+	printf("%-25s", name);
 	for(int i = 0; i < len; i++)
 		printf("%f ", f[i]);
 	printf("\n");
 }
 
+// gets int values from the device and prints them
+void print_infoi(ohmd_device* hmd, const char* name, int len, ohmd_int_value val)
+{
+	int iv[len];
+	ohmd_device_geti(hmd, val, iv);
+	printf("%-25s", name);
+	for(int i = 0; i < len; i++)
+		printf("%d ", iv[i]);
+	printf("\n");
+}
+
 int main(int argc, char** argv)
 {
 	ohmd_context* ctx = ohmd_ctx_create();
@@ -56,7 +67,7 @@ int main(int argc, char** argv)
 	int ivals[2];
 	ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
 	ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
-	printf("resolution:         %i x %i\n", ivals[0], ivals[1]);
+	printf("resolution:              %i x %i\n", ivals[0], ivals[1]);
 
 	print_infof(hmd, "hsize:",            1, OHMD_SCREEN_HORIZONTAL_SIZE);
 	print_infof(hmd, "vsize:",            1, OHMD_SCREEN_VERTICAL_SIZE);
@@ -67,6 +78,8 @@ int main(int argc, char** argv)
 	print_infof(hmd, "left eye aspect:",  1, OHMD_LEFT_EYE_ASPECT_RATIO);
 	print_infof(hmd, "right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
 	print_infof(hmd, "distortion k:",     6, OHMD_DISTORTION_K);
+	
+	print_infoi(hmd, "digital button count:", 1, OHMD_BUTTON_COUNT);
 
 	printf("\n");
 
@@ -79,6 +92,18 @@ int main(int argc, char** argv)
 		ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
 
 		print_infof(hmd, "rotation quat:", 4, OHMD_ROTATION_QUAT);
+		print_infoi(hmd, "button event count:", 1, OHMD_BUTTON_EVENT_COUNT);
+		
+		int event_count = 0;
+
+		ohmd_device_geti(hmd, OHMD_BUTTON_EVENT_COUNT, &event_count);
+
+		for(int i = 0; i < event_count; i++){
+			int event[2] = {0, 0};
+			ohmd_device_geti(hmd, OHMD_BUTTON_POP_EVENT, event);
+			printf("button %d: %s", event[0], event[1] == OHMD_BUTTON_DOWN ? "down" : "up");
+		}
+
 		ohmd_sleep(.01);
 	}
 

+ 1 - 0
src/drv_dummy/dummy.c

@@ -70,6 +70,7 @@ static ohmd_device* open_device(ohmd_driver* driver, ohmd_device_desc* desc)
 	priv->base.properties.lens_vpos = 0.046800f;
 	priv->base.properties.fov = DEG_TO_RAD(125.5144f);
 	priv->base.properties.ratio = (1280.0f / 800.0f) / 2.0f;
+	priv->base.properties.digital_button_count = 4;
 
 	// calculate projection eye projection matrices from the device properties
 	ohmd_calc_default_proj_matrices(&priv->base.properties);

+ 5 - 1
src/openhmd.c

@@ -196,18 +196,22 @@ int OHMD_APIENTRY ohmd_close_device(ohmd_device* device)
 
 	ohmd_context* ctx = device->ctx;
 	int idx = device->active_device_idx;
+	ohmdq* dinq = device->digital_input_event_queue;
 
 	memmove(ctx->active_devices + idx, ctx->active_devices + idx + 1,
 		sizeof(ohmd_device*) * (ctx->num_active_devices - idx - 1));
 
 	device->close(device);
+	
+	if(dinq)
+		ohmdq_destroy(dinq);
 
 	ctx->num_active_devices--;
 
 	for(int i = idx; i < ctx->num_active_devices; i++)
 		ctx->active_devices[i]->active_device_idx--;
 
-	ohmd_unlock_mutex(device->ctx->update_mutex);
+	ohmd_unlock_mutex(ctx->update_mutex);
 
 	return OHMD_S_OK;
 }

+ 1 - 0
src/queue.c

@@ -93,4 +93,5 @@ void ohmdq_destroy(ohmdq* me)
 {
 	free(me->elems);
 	ohmd_destroy_mutex(me->mutex);
+	free(me);
 }

+ 3 - 0
tests/unittests/queue.c

@@ -55,4 +55,7 @@ void test_ohmdq_push_pop()
 	}
 	
 	TAssert(ohmdq_get_size(q) == 0);
+
+	ohmdq_destroy(q);
+	ohmd_ctx_destroy(ctx);
 }