3

The problem is:

Edit: So, how can I point the GLFWwindow *window pointer from main, to CreateWindow() function, where glfwCreateWindow() function returns a pointer to the original pointer in main()?

Calling the function CreateWindow is causing unexpected results. After function call, original *window struct is NULL and a segmentation fault occurs.

This is the main:

int main() {
clear();    
GLFWwindow *window;

CreateWindow(window, 800, 600);
assert(window != NULL);
while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
}
DestroyWindow(window);
return 0;}

This is the called function:

void CreateWindow(GLFWwindow *window, int width, int height) {
if(!glfwInit()){
    printf("Unable to init glfw!");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(width, height,"Vulkan",NULL,NULL);
glfwSetWindowUserPointer(window, window);};

This here is the result:

test: engine_core.c:28: main: Assertion `window != NULL' failed.

[1] 3248 abort (core dumped) ./test

  • Please point me to the answer. – leafy_fingers Jul 21 '19 at 20:49
  • Did you see the answers in the linked question? – melpomene Jul 21 '19 at 20:50
  • Yep, saw the answer. I understand that I need to a pointer to pointer, but I have exhausted how it works in this case. GLFWwindow *window has to be a pointer, because the return function from glfwCreateWindow() is casting the return to a pointer. How can I point the pointer from main() to CreateWindow() function, so that glfwCreateWindow() function can return a pointer that points all the way back to main()? – leafy_fingers Jul 21 '19 at 21:18
  • That doesn't make any sense to me. What do you mean? – melpomene Jul 21 '19 at 21:20
  • Does it make any sense after edit? – leafy_fingers Jul 21 '19 at 21:23
  • Not really. Where do you see a cast? – melpomene Jul 21 '19 at 21:26
  • I think your edit matches exactly what I gave previously in the answer. – prog-fh Jul 21 '19 at 21:32
  • The function call inside CreateWindow() --> window = glfwCreateWindow() returns a cast... || return (GLFWwindow*) window; – leafy_fingers Jul 21 '19 at 21:33
  • which cast? where? Did you show us the `CreateWindow()` you actually use? – prog-fh Jul 21 '19 at 21:34
  • @prog-fh you are correct. I understand the first option, but I would like to also understand how the second option is achieved. I see the logic, but can't type the code. I don't use any casts. The CreateWindow() I show is the one I use. The cast comes from GLFW.h library. – leafy_fingers Jul 21 '19 at 21:36
  • read it in my answer. I just wrote it (reload the page in your browser if you can't see it right now) – prog-fh Jul 21 '19 at 21:38
  • Thanks!! I understand the logic behind this. And I think that option 1 is better too! – leafy_fingers Jul 21 '19 at 21:46

1 Answers1

1

Two options:

1) CreateWindow() could return the window pointer.

2) CreateWindow() could expect GLFWindow **

In the second case, you should pass &window as argument (from main()), and use *window in the definition of CreateWindow(). This will change the window pointer in the calling context (main()).

The first option is easier (my own opinion).


1)

GLFWwindow * CreateWindow(int width, int height) {
if(!glfwInit()){
    printf("Unable to init glfw!");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow(width, height,"Vulkan",NULL,NULL);
glfwSetWindowUserPointer(window, window);
return window;
}

Then in main()

GLFWwindow *window=CreateWindow(800, 600);

2)

void CreateWindow(GLFWwindow **window, int width, int height) {
if(!glfwInit()){
    printf("Unable to init glfw!");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
*window = glfwCreateWindow(width, height,"Vulkan",NULL,NULL);
glfwSetWindowUserPointer(*window, *window);
}

Then in main()

GLFWwindow *window;
CreateWindow(&window, 800, 600);
prog-fh
  • 13,492
  • 1
  • 15
  • 30