C, SDL: error: cannot convert 'SDL_Surface*' to 'SDL_Surface* (*)[15] - c

so I'm working on a code for filling a screen with a table of surfaces; here's the code:
main.c
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <SDL_image.h>
#include "maploader.h"
#define W 510
#define H 510
#define SIZE 34
void pause();
int main ( int argc, char** argv )
{
SDL_Surface *screen = NULL;
SDL_Surface *surfaces[15][15];
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(W, H, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("demon game", NULL);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
mapload(screen, surfaces[15][15], NULL);
SDL_Flip(screen);
pause();
SDL_QUIT;
return EXIT_SUCCESS;
}
void pause()
{
int continuer = 1;
SDL_Event event;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
}
}
}
maploader.c
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <SDL_image.h>
#define W 510
#define H 510
#define SIZE 34
SDL_Surface *surfaces[15][15];
void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl)
{
FILE *level = NULL;
char elements[125];
int i, j, k = 0;
SDL_Rect elementposition = {0,0};
level = fopen("level.txt", "r");
if (level == NULL)
{
exit(0);
}
fgets(elements, 125, level);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
for (i=0; i<15; i++)
{
for (j=0; j<15; j++)
{
if (elements[k] == "0")
{
surfaces[i][j] = IMG_Load("mur.jpg");
}
else if (elements[k] == "1")
{
surfaces[i][j] = IMG_Load("caisse.jpg");
}
else if (elements[k] == "2")
{
surfaces[i][j] = IMG_Load("objectif.png");
}
else
{
surfaces[i][j] = NULL;
}
k++;
}
}
for (i=0; i<15; i++)
{
for (j=0; j<15; j++)
{
SDL_BlitSurface(surfaces[i][j], NULL, screen, &elementposition);
elementposition.x += SIZE;
}
elementposition.y += SIZE;
}
}
the only error I get from compiling is the following: "cannot convert 'SDL_Surface*' to 'SDL_Surface* ()[15]' for argument '2' to 'void mapload(SDL_Surface, SDL_Surface* (*)[15], int)'|"
apparently, the error is initialized from the second argument of the mapload function, but I don't have a clear idea about what exactly is wrong. any ideas, please?

This,
mapload(screen, surfaces[15][15], NULL);
should be
mapload(screen, surfaces, NULL);
But now you should ask yourself, if you didn't know that then probably,
The signature of void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl) is altogether wrong.
You need to study what are arrays in c and their relation to pointers.
Note that surfaces[15][15] means the 16th element of the 16th array of pointers, none of which exists because you only allocated 15 of each. So you need to understand arrays in c, how they are allocated and how you can have a dynamic array.
Also, the fact that you tell a c compiler that a function is expecting an array is not very relevant inside such function, so the syntax SDL_Surface *surfaces[][15] seems strange to a c programmer.
And finally, since surfaces is a global variable you don't need to pass it as a parameter, but then you should ask yourself, should it be a global variable?

Related

not all PWMs are generated in Pi4 wiringPI language C

I am working on a project where I have to use multi-threading to generate multiple pwms. I am working on raspberry and I use WiringPI to generate softPWMs.
The problems is my raspberry does not want to generate 5 PWMs. Each time a run the code, the PWMs are generated randomly on 3 of the five pins I have setted. I do not know if it is the OS putting some threads on sleep or not.
I have no idea where the problem might be from. In fact, I tries using the same code to send periodically send many variable and it did work. But when I try using the same code with PWM it does not work. The code I am using is right below. If any of you has any idea please help
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 1
struct thArg{
int PWM_pin1;
int rapportCyclique1;
int PWM_pin2;
int rapportCyclique2;
int PWM_pin3;
int rapportCyclique3;
int PWM_pin4;
int rapportCyclique4;
};
struct thArg data = {
22,20,
23,40,
24,80,
25,60
};
void* routine(void* args){
//int PWM_pin = *(int*)args->PWM_pin;
//int intensity = *(int*)args->rapportCyclique;
//pthread_mutex_lock(&mutexBuffer);
struct thArg *arrgs = (struct thArg *) args;
int PWM_pin1 = arrgs->PWM_pin1;
int rapportCyclique1 = arrgs->rapportCyclique1;
int PWM_pin2 = arrgs->PWM_pin2;
int rapportCyclique2 = arrgs->rapportCyclique2;
int PWM_pin3 = arrgs->PWM_pin3;
int rapportCyclique3 = arrgs->rapportCyclique3;
int PWM_pin4 = arrgs->PWM_pin4;
int rapportCyclique4 = arrgs->rapportCyclique4;
pinMode(PWM_pin1, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin2, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin3, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin4, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
softPwmWrite(PWM_pin1, rapportCyclique1);
softPwmWrite(PWM_pin2, rapportCyclique2);
softPwmWrite(PWM_pin3, rapportCyclique3);
softPwmWrite(PWM_pin4, rapportCyclique4);
//pthread_mutex_unlock(&mutexBuffer);
}
void* routine(void*);
int main(int argc, char** argv)
{
wiringPiSetup();
pthread_t th[THREAD_NUM];
for(int i=0; i<THREAD_NUM; i++)
{
struct thArg *a = malloc(sizeof(struct thArg));
*a = data;
if(pthread_create(&th[i], NULL, &routine, a) != 0)
{
perror("failed to create thread\n");
exit( EXIT_FAILURE );
}
}
for(int i = 0; i<THREAD_NUM; i++){
if(pthread_join(th[i], NULL)!= 0)
{
perror("Failed to join thread\n");
exit( EXIT_FAILURE );
}
}
//pthread_mutex_destroy(&mutexBuffer);
return EXIT_SUCCESS;
}

How to achieve something like V-sync for every line in terminal emulators using C?

I'm trying to write some code which would allow to render 3D graphics in console using characters and escape sequences (for color). I need it for one specific program I want to write, but, if possible, I would like to make it more universal. I'm experiencing something like screen tearing and I want to get rid of it (that the whole screen would be printed "at once"). The test is simply displaying screen filled with spaces with wite and black background (one full white frame then one full black one) in one second interval.
I have tried:
At the begging I thought about line buffering on stdout. Tried both disabling it and creating full buffor with size sufficient enough to hold every char on the screen. Second option provides better results, and by that I mean that less frames are teared, but they still are.
I thought it might be a problem with my terminal emulator (this question gave me the idea) so I started to mess around with other ones. I've got best result with Kitty but it's not there yet.
The next thing was to mess with Kitty configuration. I've noticed that if I would increase the input_delay setting to about 20 ms the problem would be almost gone. Just few of, and not every frame would be teared.
So, I came into the conclusion that in fact terminal emulators (or at least kitty) are being too fast and there might be some sort of race condition here, where buffer is not flushed yet fully and TE display both what was partially flushed and is part of old frame. Am I wrong? If not is there any way I can enforce terminals to wait for input to finnish before displaying it, or at least enforce input delay in C?
here is the relevant part of the code:
main.c
#include "TermCTRL/termCTRL.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
int main()
{
termcell_t cell;
int k;
uint16_t x,y;
termCTRL_get_term_size(&x, &y);
sleep(1);
termCTRL_init();
uint8_t a = 0;
for(k=0; k<200; k++)
{
a^=255;
cell.bg.B = a;
cell.bg.G = a;
cell.bg.R = a;
cell.fg.B = a;
cell.fg.G = a;
cell.fg.R = a;
cell.symbol[0] = ' '; //symbol is in fact a string, because I want to use UTF chars too
cell.symbol[1] = '\0';
for(int xd=0; xd<x; xd++)
for(int yd=0; yd<y; yd++)
{
termCTRL_load_termcell(xd, yd, &cell);
}
termCTRL_update_screen();
sleep(1);
}
termCTRL_close();
return 0;
}
termCTRL.h
#pragma once
#include <stdint.h>
#define INPLACE_TERMCELL(FG_R, FG_G, FG_B, BG_R, BG_G, BG_B, SYMBOL) \
(termcell_t) { {FG_R, FG_G, FG_B}, {BG_R, BG_G, BG_B}, SYMBOL }
#define termCTRL_black_fill_screen() \
termCTRL_fill_screen(&INPLACE_TERMCELL(0, 0, 0, 0, 0, 0, " "))
typedef struct termcell_color_t
{
uint16_t R;
uint16_t G;
uint16_t B;
} termcell_color_t;
typedef struct termcell_t
{
termcell_color_t fg;
termcell_color_t bg;
char symbol[4];
} termcell_t;
typedef enum termCTRL_ERRNO
{
termCTRL_OUT_OF_BORDER = -2,
termCTRL_INVALID_TERMCELL = -1,
termCTRL_INTERNAL_ERROR = 0,
termCTRL_OK = 1,
} termCTRL_ERRNO;
void termCTRL_init();
void termCTRL_close();
void termCTRL_get_term_size(uint16_t *col, uint16_t *row);
termCTRL_ERRNO termCTRL_load_termcell(uint16_t x, uint16_t y, termcell_t *in);
void termCTRL_update_screen();
termCTRL_ERRNO termCTRL_fill_screen(termcell_t *cell);
termCTRL.c
#include "termCTRL.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define CONVERTED_TERMCELL_SIZE 44
#define CAST_SCREEN_TO_BUFFER \
char (*screen_buffer)[term_xsize][term_ysize][CONVERTED_TERMCELL_SIZE]; \
screen_buffer = _screen_buffer
static void *_screen_buffer = NULL;
static uint16_t term_xsize, term_ysize;
static char *IO_buff = NULL;
void termCTRL_get_term_size(uint16_t *col, uint16_t *row)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
*col = w.ws_col;
*row = w.ws_row;
}
void int_decompose(uint8_t in, char *out)
{
uint8_t x = in/100;
out[0] = x + '0';
in -= x*100;
x = in/10;
out[1] = x + '0';
in -= x*10;
out[2] = in + '0';
}
termCTRL_ERRNO termCTRL_move_cursor(uint16_t x, uint16_t y)
{
char mov_str[] = "\x1b[000;000H";
if(x<term_xsize && y<term_ysize)
{
int_decompose(y, &mov_str[2]);
int_decompose(x, &mov_str[6]);
if(fputs(mov_str, stdout) == EOF) return termCTRL_INTERNAL_ERROR;
else return termCTRL_OK;
}
else
{
return termCTRL_OUT_OF_BORDER;
}
}
termCTRL_ERRNO termCTRL_load_termcell(uint16_t x, uint16_t y, termcell_t *in)
{
CAST_SCREEN_TO_BUFFER;
if(in == NULL) return termCTRL_INVALID_TERMCELL;
if(x >= term_xsize || y >= term_ysize) return termCTRL_OUT_OF_BORDER;
//because screen buffer was initialized, it is only needed to replace RGB values and symbol.
//whole escape sequence is already there
int_decompose(in->fg.R, &(*screen_buffer)[x][y][7]);
int_decompose(in->fg.G, &(*screen_buffer)[x][y][11]);
int_decompose(in->fg.B, &(*screen_buffer)[x][y][15]);
int_decompose(in->bg.R, &(*screen_buffer)[x][y][26]);
int_decompose(in->bg.G, &(*screen_buffer)[x][y][30]);
int_decompose(in->bg.B, &(*screen_buffer)[x][y][34]);
strcpy(&(*screen_buffer)[x][y][38], in->symbol); //copy symbol, note that it could be UTF char
return termCTRL_OK;
}
termCTRL_ERRNO termCTRL_fill_screen(termcell_t *cell)
{
uint16_t x, y;
termCTRL_ERRNO ret;
for(y=0; y<term_ysize; y++)
for(x=0; x<term_xsize; x++)
{
ret = termCTRL_load_termcell(x, y, cell);
if(ret != termCTRL_OK)
return ret;
}
return ret;
}
void termCTRL_update_screen()
{
uint16_t x, y;
CAST_SCREEN_TO_BUFFER;
termCTRL_move_cursor(0, 0);
for(y=0; y<term_ysize-1; y++)
{
for(x=0; x<term_xsize; x++)
fputs((*screen_buffer)[x][y], stdout);
fputs("\n", stdout);
}
//last line got special treatment because it can't have \n
for(x=0; x<term_xsize; x++)
fputs((*screen_buffer)[x][y], stdout);
fflush(stdout);
}
void termCTRL_init()
{
uint16_t x, y;
termCTRL_get_term_size(&term_xsize, &term_ysize);
IO_buff = calloc(term_xsize*term_ysize, CONVERTED_TERMCELL_SIZE);
setvbuf(stdout, IO_buff, _IOFBF, term_xsize*term_ysize*CONVERTED_TERMCELL_SIZE);
_screen_buffer = calloc(term_xsize*term_ysize, CONVERTED_TERMCELL_SIZE);
fputs("\e[?25l", stdout); //hide cursor
fputs("\x1b[2J", stdout); //clear screen
CAST_SCREEN_TO_BUFFER;
for(y=0; y<term_ysize; y++)
for (x=0; x<term_xsize; x++)
sprintf( (*screen_buffer)[x][y], "\x1b[38;2;200;200;000m\x1b[48;2;000;000;000m ");
termCTRL_update_screen();
}
void termCTRL_close()
{
free(_screen_buffer);
setvbuf(stdout, NULL, _IONBF, 0);
free(IO_buff);
printf("\e[?25h"); //show cursor
printf("\x1b[m"); //reset colors
printf("\x1b[2J"); //clear screen
}

CaptureStackBackTrace inconsistencies using FramesToSkip

On windows you can capturing the stack trace using CaptureStackBackTrace as
void* frames[USHRT_MAX];
USHORT framesCount = CaptureStackBackTrace(0, USHRT_MAX, frames, NULL);
However, capturing it by smaller chunks in a loop to avoid allocating a USHRT_MAX buffer doesn't provide the same result.
This code
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
__declspec(noinline) void CheckStack(void)
{
printf("Checking stack...\n");
void* entireStack[USHRT_MAX];
USHORT frameCount = CaptureStackBackTrace(0, USHRT_MAX, entireStack, NULL);
printf("Stack size is: %u\n", frameCount);
ULONG frameOffset = 1;
for (;;)
{
void* chunk[64];
USHORT framesFound = CaptureStackBackTrace(frameOffset, 64, chunk, NULL);
if (framesFound)
{
if (memcmp(entireStack + frameOffset, chunk, sizeof(chunk)) != 0)
{
printf("Incorrect content\n");
}
frameOffset += (ULONG)framesFound;
}
else
{
break;
}
}
if (frameCount != frameOffset)
{
printf("Incorrect count (%u != %u)\n", frameCount, frameOffset);
}
printf("Done\n");
}
__declspec(noinline) void Test(int i)
{
if (i != 500)
Test(++i);
else
CheckStack();
}
int main()
{
Test(0);
}
produces the following output
Checking stack...
Stack size is: 507
Incorrect count (507 != 257)
Done
when building as cl /Od main.c /link /OUT:main.exe.
Am I using the FramesToSkip parameter incorrectly or why are the counts not equal?
If you are using Windows Server 2003 and Windows XP,
The sum of the FramesToSkip and FramesToCapture parameters must be
less than 63.
That's in document.
Else, as #RbMm says, In the API source code, there is the following logic:
if(FramesToSkip>0xfe)
{
return 0; //There are too many stack structures skipped, returning directly to 0.
}
However, this is not metioned on msdn both in the CaptureStackBackTrace and RtlCaptureStackBackTrace.
I am not going to post the source code here, but prove it in debugging:
1.Create a sample:
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
__declspec(noinline) void CheckStack(void)
{
void* entireStack[USHRT_MAX];
USHORT frameCount = CaptureStackBackTrace(255, USHRT_MAX, entireStack, NULL);
}
__declspec(noinline) void Test(int i)
{
if (i != 500)
Test(++i);
else
CheckStack();
}
int main()
{
Test(0);
}
2. Step into CaptureStackBackTrace in Disassembly:
You can see that dword ptr[ebp+8](the first parameter of CaptureStackBackTrace pushed in stack) will be compared with 0feh(254). If true, return 0.

Store filenames in array VC++

I am trying to get all the filenames into an array . But after reading all the filenames the array only have the last filename;
#include <Windows.h>
#include <strsafe.h>
#include <iostream>
int main() {
WIN32_FIND_DATA ffd;
HANDLE handle = INVALID_HANDLE_VALUE;
TCHAR *directory = L"D:/*.*";
LPCWSTR filenames[30] ;
handle = FindFirstFile(directory, &ffd);
int count = 0;
if (handle != INVALID_HANDLE_VALUE) {
OutputDebugString(ffd.cFileName);
do {
filenames[count++] = ffd.cFileName;
OutputDebugString(filenames[count -1]);
} while (FindNextFile(handle, &ffd) != 0);
}
else {
OutputDebugString(L"Nothing to display \n");
}
for (int i = 0; i < 10; i++) {
OutputDebugString(filenames[i]);
}
FindClose(handle);
getchar();
return 0;
}
What is the problem I have in this code ..
Thanks in advance..
LPCWSTR filenames[30];
Above is a character array. It is not an array of strings. It is also too short to contain a filename which can be MAX_PATH long.
Use wchar_t **buf; to create an array of strings, or use std::vector and std::string.
If file handle is invalid then do not close it.
Don't use TCHAR unless it is part of some homework or something. Just use wchar_t for Windows.
#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::wstring> vec;
wchar_t *directory = L"D:/*.*";
WIN32_FIND_DATA ffd;
HANDLE handle = FindFirstFile(directory, &ffd);
if (handle != INVALID_HANDLE_VALUE)
{
do {
vec.push_back(ffd.cFileName);
} while (FindNextFile(handle, &ffd));
FindClose(handle);
}
else
{
OutputDebugString(L"Nothing to display \n");
}
for (unsigned int i = 0; i < vec.size(); i++)
{
OutputDebugString(vec[i].c_str());
OutputDebugString(L"\n");
}
getchar();
return 0;
}

How to omit quotation marks usage in char type?

I'm having a really hard time adjusting function to my needs. First of all look at those three files and notice how I have to call f_texture function in main function in order to make it work:
externs.h
#ifndef EXTERNS_H_
#define EXTERNS_H_
extern char t_about[100];
extern int friction;
extern int f_texture(char* ,char*);
#endif
functionA.c
#include <stdio.h>
#include "externs.h"
int main()
{
f_texture("rough","friction");
printf("Friction: %d\n", friction);
f_texture("rough","t_about");
return 0;
}
functionB.c
#include "externs.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
char t_about[100];
int friction;
int f_texture(char* texture,char* what_is_needed)
{
/*Checking if both values are present*/
assert(what_is_needed);
assert(texture);
/*Static array in order to prevent it's disappearance*/
memset(t_about, '\0', sizeof(t_about));
/*Configuring variables for desired texture*/
if (strcmp(texture, "smooth") == 0)
{
strcpy(t_about, "The surface is perfectly smooth, without any "
"protuberances.\n");
friction = 0;
}
else if (strcmp(texture, "rough") == 0)
{
strcpy(t_about, "Rough bumps can be feeled under my fingertips.\n");
friction = 4;
}
/*In case of absent keyword of desired texture it will crash the program*/
else
{
assert(!what_is_needed);
}
/*Returning desired value*/
if (strcmp(what_is_needed, "t_about") == 0)
{
int i=0;
while (t_about[i] != '\0')
{
printf("%c", t_about[i]);
i++;
}
}
else if (strcmp(what_is_needed, "friction") == 0)
{
return friction;
}
/*In case of absent keyword of desired value it will crash the program*/
else
{
assert(!what_is_needed);
}
return 0;
}
And now here is my question: How to rewrite this code to make it possible to call f_texture function without using quotation marks inside? I mean instead of f_texture("abcd","efgh") just to type f_texture(abcd,efgh). I've noticed that this way it's required just after I've wrote this code.
Thanks in advance.
If you don't want to assign string constants to variables or preprocessor object macros, another option is to use preprocessor function macros, using the stringification feature:
#define call_f_texture(a,b) f_texture(#a,#b)
....
call_f_texture(rough,friction);
The C preprocessor will turn this into
f_texture("rough","friction");
You can also use some macros:
#define ROUGH "rough"
#define FRICTION "friction"
#define T_ABOUT "t_about"
int main()
{
f_texture(ROUGH, FRICTION);
printf("Friction: %d\n", friction);
f_texture(ROUGH, T_ABOUT);
return 0;
}
You can do like this,
char rough[]="rough";
char friction[]= "friction";
and call
f_texture(rough, friction);
char a[MAX] = "rouch";
char b[MAX} = "friction";
int main()
{
f_texture();
...
}
int f_texture()
{
/*Checking if both values are present*/
assert(b);
assert(a);
}
or
int f_texture(char* a,char* b)
{
/*Checking if both values are present*/
assert(b);
assert(a);
...
}
int main()
{
char a[MAX] = "rouch";
char b[MAX} = "friction";
f_texture(a,b);
...
}

Resources