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;
}
Related
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 can only get events after the window has moved, not while it is moving.
I want to get moving events in real time. Please,what should I do?
#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdio.h>
int main(int argv, char **agrc)
{
Display *dpy = XOpenDisplay(NULL);
Window w = XCreateSimpleWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)), 10, 10, 800, 400, 1, BlackPixel(dpy, DefaultScreen(dpy)), WhitePixel(dpy, DefaultScreen(dpy)));
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XEvent event;
for (;;)
{
XNextEvent(dpy, &event);
if(event.type == ConfigureNotify)
{
XConfigureEvent x = event.xconfigure;
printf("config[%d]{%d,%d,%d,%d}\n", x.window, x.x, x.y, x.width, x.height);
}
}
XDestroyWindow(dpy, w);
XCloseDisplay(dpy);
return 0;
}
I'm getting initiated to the CSFML library with macOS. But I can't display the window. The application launches well, but it goes into "does not answer" and no window is displayed...
Here is my code:
sfRenderWindow *new_window(void)
{
sfRenderWindow *window;
sfVideoMode video_mode = {1920, 1080, 32};
window = sfRenderWindow_create(video_mode,
"My Hunter",
sfDefaultStyle,
NULL);
return window;
}
int main (int argc, char **argv)
{
sfRenderWindow *window = new_window();
while (sfRenderWindow_isOpen(window)) {
sfRenderWindow_clear(window, sfBlack);
sfRenderWindow_display(window);
}
sfRenderWindow_destroy(window);
}
And my my.h:
#include <SFML/Audio.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <SFML/System.h>
#include <stdio.h>
#include <stdlib.h>
I'm trying to understand why the following program is leaking memory. When I look at the heap size by using the command more /proc/<pid>/smaps, I can see that the heap is only growing. It seems that the XtDestroyWidget is not really freeing up the memory. I would be very grateful if somebody can tell me why this is happening and show me the right way of recreating widgets.
Thanks in advance!
/* Compile on Solaris: cc widgets_mem.c -lXm -lXt -lX11 */
/* Compile on Linux: gcc -m32 widgets_mem.c -lXm -lXt -lX11 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysymdef.h>
#include <Xm/MainW.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
Widget toplevel;
Widget w_main;
Widget w_bb;
Widget w_button = NULL;
Window root_win;
XtAppContext app;
int screen_number;
Display *display = NULL;
void recreateWidgets ()
{
printf ("recreating widgets\n");
XtDestroyWidget (w_button);
w_button = XtVaCreateManagedWidget (
"button",
xmPushButtonWidgetClass, w_bb,
XmNfillOnArm, False,
XmNhighlightThickness, 0,
XmNborderWidth, 1,
XmNmarginTop, 1,
XmNmarginWidth, 2,
XmNmarginHeight, 0,
XmNwidth, 20,
XmNheight, 10,
XmNrecomputeSize, False,
XmNalignment, XmALIGNMENT_CENTER,
NULL);
}
void main (int argc, char **argv)
{
XEvent event;
toplevel = XtVaAppInitialize (&app, "Mem leak test",
NULL, 0, &argc, argv, NULL, NULL);
display = XtDisplay (toplevel);
screen_number = DefaultScreen (display);
root_win = RootWindow (display, screen_number);
w_main = XtVaCreatePopupShell (
"main",
topLevelShellWidgetClass, toplevel,
XmNgeometry, "-0+0",
XmNborderWidth, 0,
XmNshadowThickness, 0,
XmNminWidth, 1,
XmNmwmDecorations, 0,
XmNmwmFunctions, 0,
XmNwidth, 600,
XmNheight, 200,
NULL);
w_bb = XtVaCreateManagedWidget (
"",
xmBulletinBoardWidgetClass, w_main,
XmNborderWidth, 4,
XmNshadowThickness, 0,
XmNmarginWidth, 0,
XmNmarginHeight, 0,
XmNx, 0,
XmNy, 0,
XmNwidth, 500,
XmNheight, 100,
NULL);
w_button = XtVaCreateManagedWidget (
"button",
xmPushButtonWidgetClass, w_bb,
XmNfillOnArm, False,
XmNhighlightThickness, 0,
XmNborderWidth, 1,
XmNmarginTop, 1,
XmNmarginWidth, 2,
XmNmarginHeight, 0,
XmNwidth, 20,
XmNheight, 10,
XmNrecomputeSize, False,
XmNalignment, XmALIGNMENT_CENTER,
NULL);
XtPopup (w_main, XtGrabNone);
while (1)
{
XtAppNextEvent (app, &event);
if (event.type == ButtonPress)
{
printf ("Button pressed, recreating widgets\n");
recreateWidgets();
}
}
}
I found the bug in my code: I had to add a XtDispatchEvent(&event), which handles the deallocating of the widgets which are on the destory list, an internal list in Xt.
In SDL2 I want to be able to draw changes to one buffer rather than redraw the whole image to two different buffers as my setup seems to be doing. Below is a really quick test which shows the unwanted behavior:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <SDL.h>
// Compile: gcc test.c -I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2
void putPixel(SDL_Renderer *renderer, int x, int y)
{
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderDrawPoint(renderer, x, y);
}
int main(int argc, char* argv[]) {
int width = 640;
int height = 480;
SDL_Window *window = SDL_CreateWindow("Test", 0,0,width,height, 0);
if (window == NULL)
{
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
return -2;
}
SDL_SetRenderDrawBlendMode(renderer,SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for(int x=0;x<8;x++)
{
for(int y=0;y<10;y++)
{
putPixel(renderer,40+x*10,50+y);
}
SDL_RenderPresent(renderer);
sleep(1);
}
SDL_Quit();
return 0;
}
The output from this is two alternating screens. It is obviously using a double buffer which means I have to clear and redraw to get the output I want. After each cycle of the for...loop I wanted to add a line to the buffer - there should have been 8 lines at the end of the program running. In this case I got 4 on one buffer and 4 on another. I don't want to redraw the previous lines again either, hence the need for one buffer:
This uses a texture as a buffer and copies this to the screen when done.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <SDL.h>
// Compile: gcc test.c -I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2
void putPixel(SDL_Renderer *renderer, int x, int y)
{
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderDrawPoint(renderer, x, y);
}
int main(int argc, char* argv[]) {
int width = 640;
int height = 480;
SDL_Window *window = SDL_CreateWindow("Test", 0,0,width,height, 0);
if (window == NULL)
{
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if (renderer == NULL)
{
return -2;
}
SDL_SetRenderDrawBlendMode(renderer,SDL_BLENDMODE_BLEND);
/* Create texture for display */
SDL_Texture *display = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);
SDL_SetRenderTarget(renderer, display);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for(int x=0;x<8;x++)
{
SDL_SetRenderTarget(renderer, display);
for(int y=0;y<10;y++)
{
putPixel(renderer,40+x*10,50+y);
}
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderCopy(renderer, display, NULL, NULL);
SDL_RenderPresent(renderer);
sleep(1);
}
SDL_Quit();
return 0;
}
The output from this is below: