Skip to content

Commit

Permalink
examples/00-helloworld/main.c refactor to match sktech2d style
Browse files Browse the repository at this point in the history
examples/00-helloworld-vulkan/main.c update
  • Loading branch information
vinjn committed May 3, 2018
1 parent 20f1b60 commit 3cb7240
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
29 changes: 28 additions & 1 deletion examples/00-helloworld-vulkan/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ int WIDTH = 800;
int HEIGHT = 600;

GLFWwindow *window;
VkInstance instance;

#if defined(DEBUG) || defined(_DEBUG)
#define V(x) { vr = (x); if( vr != VK_SUCCESS ) { printf("%s %d %s %s", __FILE__, __LINE__, vr, L#x); } }
#else
#define V(x) { vr = (x); }
#endif

VkResult vr = VK_SUCCESS;

void setup()
{
Expand All @@ -15,7 +24,25 @@ void setup()
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", NULL, NULL);
window = glfwCreateWindow(WIDTH, HEIGHT, "helloworld-vulkan", NULL, NULL);

VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
{
appInfo.pApplicationName = "helloworld-vulkan";
appInfo.pEngineName = "https://github.com/island-org/island";
appInfo.apiVersion = VK_API_VERSION_1_0;
}

VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
{
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
instInfo.pApplicationInfo = &appInfo;
instInfo.ppEnabledExtensionNames = glfwExtensions;
instInfo.enabledExtensionCount = glfwExtensionCount;
}

V(vkCreateInstance(&instInfo, NULL, &instance));
}

void draw()
Expand Down
56 changes: 36 additions & 20 deletions examples/00-helloworld/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ static void errorcb(int error, const char* desc)
printf("GLFW error %d: %s\n", error, desc);
}

int main()
void setup()
{
if (!glfwInit())
{
printf("Failed to init GLFW.");
return -1;
exit(1);
}

glfwSetErrorCallback(errorcb);
Expand All @@ -31,7 +31,7 @@ int main()
if (!window)
{
glfwTerminate();
return NULL;
exit(1);
}
glfwMakeContextCurrent(window);

Expand All @@ -40,32 +40,48 @@ int main()
if (glewInit() != GLEW_OK)
{
printf("Error: %s\n", glewGetErrorString(err));
return -1;
exit(1);
}
// GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
glGetError();
}

while (!glfwWindowShouldClose(window))
{
double mx, my;
int winWidth, winHeight;
int fbWidth, fbHeight;
float pxRatio;
void draw()
{
double mx, my;
int winWidth, winHeight;
int fbWidth, fbHeight;
float pxRatio;

glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;
glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;

// Update and render
glViewport(0, 0, fbWidth, fbHeight);
glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
// Update and render
glViewport(0, 0, fbWidth, fbHeight);
glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}

void teardown()
{
glfwDestroyWindow(window);
glfwTerminate();
}

int main()
{
setup();

while (!glfwWindowShouldClose(window))
{
draw();

glfwSwapBuffers(window);
glfwPollEvents();
}

glfwTerminate();
teardown();
}

0 comments on commit 3cb7240

Please sign in to comment.