Example #1
0
int main( void )
{
   GLFWwindow* window;

   /* Init GLFW */
   if( !glfwInit() )
      exit( EXIT_FAILURE );

   window = glfwCreateWindow( 400, 400, "Boing (classic Amiga demo)", NULL, NULL );
   if (!window)
   {
       glfwTerminate();
       exit( EXIT_FAILURE );
   }

   glfwSetWindowAspectRatio(window, 1, 1);

   glfwSetFramebufferSizeCallback(window, reshape);
   glfwSetKeyCallback(window, key_callback);
   glfwSetMouseButtonCallback(window, mouse_button_callback);
   glfwSetCursorPosCallback(window, cursor_position_callback);

   glfwMakeContextCurrent(window);
   gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
   glfwSwapInterval( 1 );

   glfwGetFramebufferSize(window, &width, &height);
   reshape(window, width, height);

   glfwSetTime( 0.0 );

   init();

   /* Main loop */
   for (;;)
   {
       /* Timing */
       t = glfwGetTime();
       dt = t - t_old;
       t_old = t;

       /* Draw one frame */
       display();

       /* Swap buffers */
       glfwSwapBuffers(window);
       glfwPollEvents();

       /* Check if we are still running */
       if (glfwWindowShouldClose(window))
           break;
   }

   glfwTerminate();
   exit( EXIT_SUCCESS );
}
Example #2
0
File: ctx.c Project: cloudhead/px
bool ctx_init(struct context *ctx, int w, int h, bool debug)
{
	assert(! ctx->win);

	glfwSetErrorCallback(error_callback);

	if (! glfwInit()) {
		return false;
	}

	GLFWmonitor          *monitor = glfwGetPrimaryMonitor();
	const GLFWvidmode    *mode    = glfwGetVideoMode(monitor);

	int mw, mh;
	glfwGetMonitorPhysicalSize(monitor, &mw, &mh);

	ctx->dpi            = mode->width / (mw / 25.4);
	ctx->hidpi          = ctx->dpi > 180.0;
	ctx->vidmode        = mode;

	if (ctx->hidpi) {
		w *= 2;
		h *= 2;
	}

	glfwWindowHint(GLFW_RESIZABLE, true);
	glfwWindowHint(GLFW_SRGB_CAPABLE, true);

	// Require OpenGL 3.3 or later
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

	// Only support new core functionality
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
	glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);

	glfwWindowHint(GLFW_SAMPLES, 16);

	if ((ctx->win = glfwCreateWindow(w, h, "px", NULL, NULL)) == NULL) {
		return false;
	}
	glfwMakeContextCurrent(ctx->win);
	glfwSetWindowUserPointer(ctx->win, ctx);
	glfwSetInputMode(ctx->win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
	glfwSetInputMode(ctx->win, GLFW_STICKY_KEYS, true);
	glfwSetKeyCallback(ctx->win, key_callback);
	glfwSetMouseButtonCallback(ctx->win, mouse_button_callback);
	glfwSetCursorPosCallback(ctx->win, cursor_pos_callback);
	glfwSetCursorEnterCallback(ctx->win, focus_callback);
	glfwSetFramebufferSizeCallback(ctx->win, framebuffer_size_callback);
	glfwSetWindowPosCallback(ctx->win, window_pos_callback);
	glfwSetCharCallback(ctx->win, char_callback);
	glfwSetWindowAspectRatio(ctx->win, w, h);

	glfwGetFramebufferSize(ctx->win, &ctx->winw, &ctx->winh);
	ctx_win_center(ctx);
	gl_init(ctx->winw, ctx->winh, debug);

	int vw = ctx->winw, /* Virtual screen */
	    vh = ctx->winh;

	if (ctx->hidpi) {
		vw /= 2;
		vh /= 2;

		/* We can't create odd-sized framebuffer textures, so we make
		 * the screen framebuffer even in case the virtual screen isn't. */
		if (vw % 2 != 0) vw ++;
		if (vh % 2 != 0) vh ++;
	}
	infof("ctx", "real screen size: %dx%d", ctx->winw, ctx->winh);
	infof("ctx", "virtual screen size: %dx%d", vw, vh);

	ctx->lastframe      = 0;
	ctx->frametime      = 0;
	ctx->transforms     = NULL;
	ctx->ortho          = mat4ortho(ctx->winw, ctx->winh);
	ctx->font           = malloc(sizeof(*ctx->font));

	ctx->screen         = framebuffer_screen(vw, vh, NULL);
	ctx->width          = ctx->hidpi ? ctx->winw / 2 : ctx->winw;
	ctx->height         = ctx->hidpi ? ctx->winh / 2 : ctx->winh;

	ctx_update_cursor_pos(ctx);

	ctx_blend_alpha(ctx);
	ctx_save(ctx);

	glfwSetTime(0);

	infof("ctx", "dpi = %f", ctx->dpi);

	return true;
}