1
General / Re: How to draw VertexArray ?
« on: December 26, 2017, 02:24:04 pm »Most examples online are in C++ and not C. The error is clear, you need to pass a pointer to render states to that function.
Hi, thanks for ur reply.
I found the problem but now my program segfault when I set texture to my RenderState (L.42: states->texture = texture;).
Error :
(click to show/hide)
[Takoo@localhost my_runner_2017]$ valgrind ./my_runner
[Takoo@localhost my_runner_2017]$ valgrind ./my_runner
==4788== Memcheck, a memory error detector
==4788== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4788== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4788== Command: ./my_runner
==4788==
==4788== Invalid write of size 8
==4788== at 0x400DAF: main (main.c:42)
==4788== Address 0x40 is not stack'd, malloc'd or (recently) free'd
==4788==
==4788==
==4788== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==4788== Access not within mapped region at address 0x40
==4788== at 0x400DAF: main (main.c:42)
==4788== If you believe this happened as a result of a stack
==4788== overflow in your program's main thread (unlikely but
==4788== possible), you can try to increase the size of the
==4788== main thread stack using the --main-stacksize= flag.
==4788== The main thread stack size used in this run was 8388608.
==4788==
==4788== HEAP SUMMARY:
==4788== in use at exit: 69,045,346 bytes in 7,012 blocks
==4788== total heap usage: 22,750 allocs, 15,738 frees, 129,593,621 bytes allocated
==4788==
==4788== LEAK SUMMARY:
==4788== definitely lost: 96 bytes in 2 blocks
==4788== indirectly lost: 0 bytes in 0 blocks
==4788== possibly lost: 68,644,326 bytes in 5,675 blocks
==4788== still reachable: 400,924 bytes in 1,335 blocks
==4788== suppressed: 0 bytes in 0 blocks
==4788== Rerun with --leak-check=full to see details of leaked memory
==4788==
==4788== For counts of detected and suppressed errors, rerun with: -v
==4788== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
[Takoo@localhost my_runner_2017]$ valgrind ./my_runner
==4788== Memcheck, a memory error detector
==4788== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4788== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4788== Command: ./my_runner
==4788==
==4788== Invalid write of size 8
==4788== at 0x400DAF: main (main.c:42)
==4788== Address 0x40 is not stack'd, malloc'd or (recently) free'd
==4788==
==4788==
==4788== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==4788== Access not within mapped region at address 0x40
==4788== at 0x400DAF: main (main.c:42)
==4788== If you believe this happened as a result of a stack
==4788== overflow in your program's main thread (unlikely but
==4788== possible), you can try to increase the size of the
==4788== main thread stack using the --main-stacksize= flag.
==4788== The main thread stack size used in this run was 8388608.
==4788==
==4788== HEAP SUMMARY:
==4788== in use at exit: 69,045,346 bytes in 7,012 blocks
==4788== total heap usage: 22,750 allocs, 15,738 frees, 129,593,621 bytes allocated
==4788==
==4788== LEAK SUMMARY:
==4788== definitely lost: 96 bytes in 2 blocks
==4788== indirectly lost: 0 bytes in 0 blocks
==4788== possibly lost: 68,644,326 bytes in 5,675 blocks
==4788== still reachable: 400,924 bytes in 1,335 blocks
==4788== suppressed: 0 bytes in 0 blocks
==4788== Rerun with --leak-check=full to see details of leaked memory
==4788==
==4788== For counts of detected and suppressed errors, rerun with: -v
==4788== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
(click to show/hide)
#include "../include/my.h"
int main()
{
int width = 5;
int height = 3;
sfVideoMode mode = {160, 96, 32};
sfRenderWindow *window = sfRenderWindow_create(mode, "MY_RUNNER", sfResize | sfClose, NULL);
int lvl[] =
{
2, 2, 2, 2, 2,
2, 2, 1, 2, 1,
0, 0, 0, 0, 0,
};
sfTexture *texture = sfTexture_createFromFile("img/bg0.jpg", NULL);
sfVertexArray *vertex = sfVertexArray_create();
sfVertexArray_setPrimitiveType(vertex, sfQuads);
sfVertexArray_resize(vertex, width * height * 4);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int nbr = lvl[i + j * width];
sfVertex *quad = sfVertexArray_getVertex(vertex, (i + j * width) * 4);
quad[0].position = (sfVector2f){i * 32, j * 32};
quad[1].position = (sfVector2f){(i + 1) * 32, j * 32};
quad[2].position = (sfVector2f){(i + 1) * 32, (j + 1) * 32};
quad[3].position = (sfVector2f){i * 32, (j + 1) * 32};
quad[0].texCoords = (sfVector2f){nbr * 32, 0};
quad[1].texCoords = (sfVector2f){(nbr + 1) * 32, 0};
quad[2].texCoords = (sfVector2f){(nbr + 1) * 32, 32};
quad[3].texCoords = (sfVector2f){nbr * 32, 32};
}
}
sfRenderStates *states = NULL;
states->texture = texture; << SEGFAULT HERE <<
while (sfRenderWindow_isOpen(window))
{
sfEvent event;
while (sfRenderWindow_pollEvent(window, &event))
{
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
}
sfRenderWindow_clear(window, sfBlack);
sfRenderWindow_drawVertexArray(window, vertex, states);
sfRenderWindow_display(window);
}
sfTexture_destroy(texture);
sfVertexArray_destroy(vertex);
sfRenderWindow_destroy(window);
return (0);
}
int main()
{
int width = 5;
int height = 3;
sfVideoMode mode = {160, 96, 32};
sfRenderWindow *window = sfRenderWindow_create(mode, "MY_RUNNER", sfResize | sfClose, NULL);
int lvl[] =
{
2, 2, 2, 2, 2,
2, 2, 1, 2, 1,
0, 0, 0, 0, 0,
};
sfTexture *texture = sfTexture_createFromFile("img/bg0.jpg", NULL);
sfVertexArray *vertex = sfVertexArray_create();
sfVertexArray_setPrimitiveType(vertex, sfQuads);
sfVertexArray_resize(vertex, width * height * 4);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int nbr = lvl[i + j * width];
sfVertex *quad = sfVertexArray_getVertex(vertex, (i + j * width) * 4);
quad[0].position = (sfVector2f){i * 32, j * 32};
quad[1].position = (sfVector2f){(i + 1) * 32, j * 32};
quad[2].position = (sfVector2f){(i + 1) * 32, (j + 1) * 32};
quad[3].position = (sfVector2f){i * 32, (j + 1) * 32};
quad[0].texCoords = (sfVector2f){nbr * 32, 0};
quad[1].texCoords = (sfVector2f){(nbr + 1) * 32, 0};
quad[2].texCoords = (sfVector2f){(nbr + 1) * 32, 32};
quad[3].texCoords = (sfVector2f){nbr * 32, 32};
}
}
sfRenderStates *states = NULL;
states->texture = texture; << SEGFAULT HERE <<
while (sfRenderWindow_isOpen(window))
{
sfEvent event;
while (sfRenderWindow_pollEvent(window, &event))
{
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
}
sfRenderWindow_clear(window, sfBlack);
sfRenderWindow_drawVertexArray(window, vertex, states);
sfRenderWindow_display(window);
}
sfTexture_destroy(texture);
sfVertexArray_destroy(vertex);
sfRenderWindow_destroy(window);
return (0);
}