I'm having trouble getting my image to display using SDL. I've checked the file path, made sure the image dimensions are compatible with the window size, and verified that the image is not NULL, but it still won't show up. I've been trying to debug this for hours and I'm at a loss. Can someone please take a look at my code and help me figure out what I'm missing? Here's the relevant code:
game.c:
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "header/game.h"
#include "header/constant.h"
SDL_Rect position, positionPlayer;
// Game.c
void game(/*SDL_Window* window, */SDL_Renderer* renderer){
SDL_Texture *bomber[4]={NULL};
SDL_Texture *bomberCurrent=NULL;
positionPlayer.x=33;
positionPlayer.y=33;
SDL_Event event;
int runningGame=1;
int i=0/*, j=0*/;
bomber[DOWN]=IMG_LoadTexture(renderer,"playerPink1.png");
if (!bomber[DOWN]) {
printf("Error loading image: %s\n", SDL_GetError());
return;
}
int w, h;
SDL_QueryTexture(bomber[DOWN], NULL, NULL, &w, &h);
if (w > 952 || h > 442) {
printf("Error: Image size is larger than window size\n");
return;
}
bomberCurrent = bomber[DOWN];
while(runningGame){
SDL_WaitEvent(&event); // Bloque l'exécution du programme jusqu
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
runningGame = 0;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
position.x=positionPlayer.x*TAILLE_BLOC;
position.y=positionPlayer.y*TAILLE_BLOC;
SDL_RenderCopy(renderer,bomberCurrent,NULL,&position);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
for(i=0;i<4;i++){
SDL_DestroyTexture(bomber[i]);
}
}
game.h:
#ifndef CONSTANT_H_INCLUDED
#define CONSTANT_H_INCLUDED
#define TAILLE_BLOC 34
enum{UP,DOWN,LEFT,RIGHT};
enum{VIDE,WALL,BOMBER};
#endif
main.c:
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "header/game.h"
int main(){
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *menu = NULL;
SDL_Event event;
SDL_Surface *icon;
int runningGame=1;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Etna Bomber", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 952, 442, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, 0);
icon = IMG_Load("src/favicon.bmp");
SDL_SetWindowIcon(window, icon);
menu = IMG_LoadTexture(renderer, "src/menu.png");
SDL_FreeSurface(icon);
while(runningGame){
SDL_WaitEvent(&event);
switch(event.type){
case SDL_QUIT:
runningGame=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
runningGame=0;
break;
case SDLK_SPACE:
game(/*window,*/renderer);
default:
break;
}
break;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, menu, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(menu);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
constant.h:
#ifndef CONSTANT_H_INCLUDED
#define CONSTANT_H_INCLUDED
#define TAILLE_BLOC 34
enum{UP,DOWN,LEFT,RIGHT};
enum{VIDE,WALL,BOMBER};
#endif
I tried using the SDL_QueryTexture function to check if the dimensions of the image were compatible with the dimensions of the window, and also verified that the image was not NULL after loading it using IMG_LoadTexture. I was expecting the image to be displayed on the window at the specified position, but it is not showing up. I also tried various other methods such as checking the file path and making sure the image is in the correct format, but the problem persists.
I am trying to create a window in SDL2 using the SDL_CreateWindow() function.
The window created by the SDL_CreateWindow() function in my code has no window decorations.
I am using GNOME Mutter.
Running the output of the code below from my terminal creates a blank window with no title bar
#include <stdlib.h>
#include <SDL2/SDL.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = NULL;
window = SDL_CreateWindow(
"Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
0
);
SDL_Delay(2000);
SDL_Quit();
return EXIT_SUCCESS;
}
I want the window created by SDL_CreateWindow() to have a title bar and an exit button
I need to move pointer and perform a click. It is moving ok but the click are not working, can anyone see what I'm missing? Code is below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(int argc, char *argv[]){
Display *dpy;
Window root_window;
dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 180, 500);
XGrabPointer(dpy, root_window, True, ButtonPressMask, GrabModeAsync,GrabModeAsync, None, None, CurrentTime);
XFlush(dpy);
return 0;
}
In a purely GDK 3.10 (no GTK) project how do I refresh/clear/redraw a transparent background of a GdkWindow?
This test.c sets the background correctly on initialization (gdk_window_show()) and when I iconify+deiconify the window but not on resizing or moving it around per mouse:
#include <gdk/gdk.h>
// #include <gdk/gdkx.h> // GDK X11
// #include <X11/Xlib.h> // Xlib
// #include <X11/Xutil.h>
void eventHandler(GdkEvent *evt, gpointer data) {
GdkWindow *win = (GdkWindow *)data;
switch(evt->type)
{
case GDK_CONFIGURE: {
// refresh window background here
printf("refresh background here\n");
break;
}
case GDK_DELETE: {
gdk_window_destroy(win);
_exit(0);
break;
}
default:
break;
}
}
int main(int argc, char *argv[]) {
gdk_init(NULL, NULL);
GdkDisplay *disp=gdk_display_get_default();
GdkScreen *scr = gdk_display_get_default_screen (disp);
GdkWindow *root = gdk_screen_get_root_window(scr);
GdkWindowAttr attr;
attr.width=200;
attr.height=200;
attr.x=0;
attr.y=0;
attr.window_type = GDK_WINDOW_TOPLEVEL;
attr.wclass=GDK_INPUT_OUTPUT;
GdkVisual *vis = gdk_screen_get_rgba_visual (scr);
attr.visual = vis;
GdkWindow *newWin=gdk_window_new(root,&attr, GDK_WA_X | GDK_WA_Y);
GdkRGBA color = { .red=1.0, .green=0.0, .blue=1.0, .alpha=0.0};
gdk_window_set_background_rgba(newWin, &color);
gdk_event_handler_set (eventHandler, newWin, NULL);
gdk_window_show(newWin);
GMainLoop *mainloop = g_main_new (TRUE);
g_main_loop_run (mainloop);
gdk_display_close(disp);
return 0;
}
gcc build command:
gcc -o test test.c `pkg-config gdk-3.0 --libs --cflags`
Outcome:
Note: I'm targeting X11 so I'm also allowed to include Xlib or GDK X11 backend functions.
Thank you for any help or pointers.
The solution to this is a fix of a rather silly mistake I made passing an incomplete attribute mask to gdk_window_new().
To make it work the line in question has to be changed to:
GdkWindow *newWin=gdk_window_new(root,&attr, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_WMCLASS);
dude here im gonna create client and combine with GIO Channel, and after i put it all together, it seems appears to work on socket, but the g_io_channel not as watching, like crashing or such..
please see following code :
#include <stdio.h>
#include <gio/gio.h> // g_timeout_add
#include <gtk/gtk.h> // gtk
#include <netinet/in.h> //sockaddr_in
#include <sys/socket.h> // socket();
#include <arpa/inet.h> // inet_addr();
#include <string.h> // memset();
struct dada
{
gint id_sock;
guint id_gio_watch;
};
gboolean incoming(GIOChannel *chan, GIOCondition condition, struct dada *didi )
{
int byte;
int insock = g_io_channel_unix_get_fd(chan);
#define MAXMAX 128
char buff[128];
printf("sock : %d\n",insock);
byte = recv(insock,buff,MAXMAX-1,0);
if(byte <= 0)
{
perror("recv");
close(didi->id_sock);
g_source_remove(didi->id_gio_watch);
return FALSE;
}
else
{
buff[byte] = '\0';
printf("coming : %s",buff);
}
return TRUE;
}
// gtk area
void hello(GtkWidget *widget, gpointer data)
{
g_print("Haii world %s\n", (char *)data);
}
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_print("a delete event has been occured properly :D\n");
return(0);
}
void destroy(GtkWidget * widget, gpointer data)
{
gtk_main_quit();
}
// end of gtk area
int main(int argc, char **argv)
{
//gtk bussines from here
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL);
gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);
button = gtk_button_new_with_label("ohayo");
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(hello), (gpointer)"hha" );
gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window));
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(button);
gtk_widget_show(window);
//gtk bussiness done here...
// network code //
struct dada didi;
memset(&didi,0,sizeof(didi));
struct sockaddr_in my; // set my network device info
gint rootsock; // handle the root socket
//socket
rootsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//binding
memset(&my,0,sizeof(my));
my.sin_addr.s_addr = inet_addr("127.0.0.1");
my.sin_family = AF_INET;
my.sin_port = htons(1111);
//bind(rootsock,(struct sockaddr*)&my,sizeof(my));
printf("sock : %d\n",rootsock);
connect(rootsock,(struct sockaddr*)&my,sizeof(my));
didi.id_sock = rootsock;
didi.id_gio_watch = g_io_add_watch(g_io_channel_unix_new(didi.id_sock),G_IO_IN|G_IO_OUT,(GIOFunc)incoming,&didi);
// network code //
gtk_main();
return 0;
}
compiling :
$ gcc -o konek_gioglib konek_gioglib.c `pkg-config glib-2.0 --libs --cflags gtk+-2.0`
my own pc run as server with port 1111 and stream connection ( TCP ) :
$ nc -v -l 1111
running my app :
$ ./konek_gioglib
sock : 6
sock : 6
server got connection and send some word :
$ nc -v -l 1111
Connection from 127.0.0.1 port 1111 [tcp/*] accepted
a
a
and when the server send something, gtk window show but with error like these :
is there anyone dont mind to explain, why these things could happened to mine ?
well last night I brainstormed myself with my own heart, and finally could make these thing work,
here the proper code
#include <stdio.h>
#include <gio/gio.h> // g_timeout_add
#include <gtk/gtk.h> // gtk
#include <netinet/in.h> //sockaddr_in
#include <sys/socket.h> // socket();
#include <arpa/inet.h> // inet_addr();
#include <string.h> // memset();
#include <fcntl.h>
#include <stdlib.h>
struct dada
{
gint id_sock;
guint id_gio_connect;
guint id_gio_watch;
};
gboolean readdata(GIOChannel *chan,GIOCondition condition, struct dada *didi)
{
gchar dada[20] = {0};
int dadaz =0;
if( condition != G_IO_IN )
return FALSE;
if(dadaz = recv(g_io_channel_unix_get_fd(chan),dada,19,0)<=0)
{
perror("recv");
close(didi->id_sock);
g_source_remove(didi->id_gio_connect);
g_source_remove(didi->id_gio_watch);
exit(0);
return FALSE;
}
printf("data in : %s\n",dada);
return TRUE;
}
gboolean incoming(GIOChannel *chan, GIOCondition condition, struct dada *didi )
{
if( condition & G_IO_ERR || condition & G_IO_HUP )
return FALSE;
didi->id_gio_watch = g_io_add_watch(chan,G_IO_IN | G_IO_ERR | G_IO_HUP,(GIOFunc)readdata,didi);
return FALSE;
}
// gtk area
void hello(GtkWidget *widget, gpointer data)
{
g_print("Haii world %s\n", (char *)data);
}
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_print("a delete event has been occured properly :D\n");
return(0);
}
void destroy(GtkWidget * widget, gpointer data)
{
gtk_main_quit();
}
// end of gtk area
int main(int argc, char **argv)
{
//gtk bussines from here
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL);
gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);
button = gtk_button_new_with_label("ohayo");
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(hello), (gpointer)"hha" );
gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window));
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(button);
gtk_widget_show(window);
//gtk bussiness done here...
// network code //
struct dada didi;
memset(&didi,0,sizeof(didi));
struct sockaddr_in your; // set my network device info
gint rootsock; // handle the root socket
//socket
rootsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
memset(&your,0,sizeof(your));
printf("sock : %d\n",rootsock);
your.sin_family = AF_INET;
your.sin_addr.s_addr = inet_addr("127.0.0.1");
your.sin_port = htons(1111);
connect(rootsock,(struct sockaddr*)&your,sizeof(your));
didi.id_sock = rootsock;
didi.id_gio_connect = g_io_add_watch(g_io_channel_unix_new(didi.id_sock),G_IO_IN | G_IO_OUT | G_IO_ERR | G_IO_HUP,(GIOFunc)incoming,&didi);
// network code //
gtk_main();
return 0;
}
and the only different had made is the "worked" code , need :
- gio for connecting, and gio for watching
instead of be "non-worked" code ( only gio for connecting ), but again i just wondering "why", why on connect() need two gio (recursively) in order make these "gio things" work,
these is really odd, if I see back then on g_io_channel + socket = server , still just get one client ? in C language
which on accept() just need only one gio and only for watching.
if someone could explain these, how great it'll be :)