I would like to integrate images on a second window of my HMI. But it gives me black screens. I loaded the images, created the canvases, and I integrated them normally. I also put them in the right format (at the size of my canvas) but nothing helps, it doesn't work.
I tried to put a fl_enable_png back, which didn't work either, I've attached my code below.
#include <stdio.h>
#include <stdlib.h>
#include <forms.h>
#include <flimage.h>
#include "poinconneuse.h"
FL_FORM *form;
FL_FORM *menu_interaction;
FL_OBJECT *obj, *quit, *logs, *bouton_retour, *id, *pwd, *login, *frame, *frame_id, *frame_pwd, *explorer, *usb_menu, *transferer_menu; /*pour menus d'acceuil*/
FL_OBJECT *importer, *exporter, *supprimer, *transferer, *text_menu[4], *bouton_menu[4];/*pour menu intéractions*/
FL_OBJECT *vaub_canv, *canv_menu[5];/*pour image*/
FL_IMAGE *icones_menu[5];
FL_IMAGE *logo_vauban, *logo_delete, *logo_importer, *logo_envoyer, *logo_transferer; /*pour les images*/
char menu_actuel[60];
int i;
/*Callback fenêtre de connexion a réseau*/
void callback_menu(FL_OBJECT *login, long data)
{
fl_hide_form(form);
fl_show_form(menu_interaction, FL_PLACE_CENTER, FL_FULLBORDER, "Interactions");
for ( i = 0; i < 4; i++)
{
flimage_display(icones_menu[i], FL_ObjWin(canv_menu[i]));
}
sprintf(menu_actuel, "Intéraction");
}
void callback_id(FL_OBJECT *id, long data)
{
}
void callback_pwd(FL_OBJECT *pwd, long data)
{
}
void callback_retour(FL_OBJECT *bouton_retour, long data)
{
fl_hide_form(menu_interaction);
fl_show_form(form, FL_PLACE_CENTER, FL_FULLBORDER, 0);
}
void callback_importerusb(FL_OBJECT *usb_menu, long data)
{
fl_show_fselector("Rechercher le nom de votre fichier", "/home/kali/Desktop", "*.c", "");
}
void callback_transferer_acceuil(FL_OBJECT *transferer_menu, long data)
{
fl_show_fselector("Rechercher le nom de votre fichier", "/home/kali/Desktop/projet", "*.c", "");
}
void callback_boutoninterac(FL_OBJECT *bouton_menu, long data)
{
switch(data)
{
case 0: /* bouton importer */
fl_show_fselector("Rechercher le nom de votre fichier", "/home/kali/Desktop/projet", "*.c", "");
break;
case 1: /* bouton exporter */
fl_show_fselector("Rechercher le nom de votre fichier", "/home/kali/Desktop/projet", "*.c", "");
break;
case 2: /* bouton supprimer */
fl_show_fselector("Rechercher le nom de votre fichier", "/home/kali/Desktop/projet", "*.c", "");
break;
case 3: /* bouton transférer */
fl_show_fselector(" le nom de votre fichier", "/home/kali/Desktop/projet", "*.c", "");
break;
}
int main(int argc, char *argv[])
{
FL_FORM *form;
fl_initialize(&argc, argv, 0, 0, 0);
form = fl_bgn_form(FL_UP_BOX, 800, 480);
fl_set_form_background_color(form, FL_LEFT_BCOL);
vaub_canv = fl_add_canvas(FL_NORMAL_CANVAS, 550, 120, 150, 114,"");
flimage_enable_png();
frame = fl_add_box(FL_NO_BOX, 200, 20, 450, 40, "T\xE9l\xE9versement vers la poinconneuse ");
fl_set_object_lsize(frame, FL_LARGE_SIZE);
fl_set_object_lstyle(frame, FL_TIMESBOLD_STYLE);
logs = fl_add_box(FL_ROUNDED3D_UPBOX, 45, 85, 710, 370, 0);
fl_set_object_color(logs, FL_WHITE, FL_WHITE);
id = fl_add_input(FL_NORMAL_INPUT, 100, 150, 350, 50, "");
fl_set_object_color(id, FL_WHITE, FL_WHITE);
fl_set_object_lsize(id, FL_LARGE_SIZE);
fl_set_object_callback(id, callback_id, 0);
frame_id = fl_add_box(FL_NO_BOX, 100, 110, 120, 30, "Utilisateur");
fl_set_object_lsize(frame_id, FL_LARGE_SIZE);
fl_set_object_lstyle(frame_id, FL_TIMESBOLD_STYLE);
pwd = fl_add_input(FL_SECRET_INPUT, 100, 260, 350, 50, "");
fl_set_object_color(pwd, FL_WHITE, FL_WHITE);
fl_set_object_lsize(pwd, FL_LARGE_SIZE);
fl_set_object_callback(pwd, callback_pwd, 0);
frame_pwd = fl_add_box(FL_NO_BOX, 100, 225, 150, 30, "Mot de passe");
fl_set_object_lsize(frame_pwd, FL_LARGE_SIZE);
fl_set_object_lstyle(frame_pwd, FL_TIMESBOLD_STYLE);
login = fl_add_button(FL_NORMAL_BUTTON, 560, 260, 100, 50, "Se connecter");
fl_set_object_color(login, FL_SPRINGGREEN, FL_MCOL);
fl_set_object_lcolor(login, FL_BLACK);
fl_set_object_callback(login, callback_menu, 0);
usb_menu = fl_add_button(FL_ROUND3DBUTTON, 150, 375, 175, 50, "Importer depuis une\n cl\xE9 usb");
fl_set_object_color(usb_menu, FL_WHITE, FL_MCOL);
fl_set_object_lcolor(usb_menu, FL_BLACK);
fl_set_object_callback(usb_menu, callback_importerusb, 0);
transferer_menu = fl_add_button(FL_ROUND3DBUTTON, 450, 375, 175, 50, "Transf\xE9rer un fichier\n vers la machine");
fl_set_object_color(transferer_menu, FL_WHITE, FL_MCOL);
fl_set_object_lcolor(transferer_menu, FL_BLACK);
fl_set_object_callback(transferer_menu,callback_transferer_acceuil, 0);
logo_vauban = flimage_load("logo_vauban.png");
fl_end_form();
menu_interaction = fl_bgn_form(FL_UP_BOX, 800, 480);
fl_set_form_background_color(menu_interaction, FL_LEFT_BCOL);
int i;
char nom_menu[4][60] = {"IMPORTER", "EXPORTER", "SUPPRIMER", "TRANSFERER"};
char label[60];
icones_menu[0] = flimage_load("importer.png");
icones_menu[1] = flimage_load("exporter.png");
icones_menu[2] = flimage_load("delete.png");
icones_menu[3] = flimage_load("transferer.png");
for (i = 0; i < 4; i++)
{
int y, x;
if ( i <= 1 )
{
y = 30;
x = 60;
if ( i == 1 )
x = 400;
}
if( i > 1)
{
y = 250;
x = 60;
if ( i == 3)
x = 400;
}
canv_menu[i] = fl_add_canvas(FL_NORMAL_CANVAS, 50 + x , y, 175, 145, "");
sprintf(label, "%s", nom_menu[i]);
bouton_menu[i] = fl_add_button(FL_NORMAL_BUTTON, 50 + x, y, 175, 165, "");
fl_set_object_color(bouton_menu[i], FL_WHITE, FL_WHITE);
fl_set_object_callback(bouton_menu[i], callback_boutoninterac, i);
text_menu[i] = fl_add_text(FL_NORMAL_TEXT, 75 + x, y + 175, 110, 30, label);
fl_set_object_color(text_menu[i], FL_LEFT_BCOL, FL_LEFT_BCOL);
fl_set_object_align(text_menu[i], FL_ALIGN_CENTER);
fl_set_object_lstyle(text_menu[i], FL_TIMESBOLD_STYLE);
}
bouton_retour = fl_add_button(FL_NORMAL_BUTTON, 680, 15, 100, 75, "Page de\n connexion");
fl_set_object_color(bouton_retour, FL_LEFT_BCOL, FL_LEFT_BCOL);
fl_set_object_callback(bouton_retour, callback_retour, 0);
fl_end_form();
fl_show_form(form, FL_PLACE_CENTER, FL_FULLBORDER, NULL);
flimage_display(logo_vauban, FL_ObjWin(vaub_canv));
fl_do_forms();
fl_hide_form(form);
fl_finish();
return 0;
}
Related
I have an issue with my code. I don't manage to change the opacity of my RenderDrawColor.
Indeed, I know that SetRenderDrawColor is defined by (renderer, R, G, B, a --> which represents the opacity value). Nevertheless, I tried to change a value but my color doesn't change and I have my rectangle fully fill again.
Here a photo of what I have with my code :
Screen that I have with my code
Here is my code, that I execute with a main.c script :
void display_end_menu(SDL_Renderer *renderer) {
if (SDL_SetRenderDrawColor(renderer, 0, 255, 0, 30) != 0) {
return_error("Impossible de modifier la couleur du render");
SDL_Quit();
}
SDL_Rect shadow_screen;
shadow_screen.w = 1280;
shadow_screen.h = 720;
shadow_screen.x = (SCREEN_WIDTH - shadow_screen.w)/2;
shadow_screen.y = (SCREEN_HEIGHT - shadow_screen.h)/2;
if (SDL_RenderFillRect(renderer, &shadow_screen) != 0) {
return_error("Impossible de dessiner le rectangle dans le render");
SDL_Quit();
}
if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30) != 0) {
return_error("Impossible de modifier la couleur du render");
SDL_Quit();
}
SDL_Rect quit_button;
quit_button.w = 200;
quit_button.h = 50;
quit_button.x = (SCREEN_WIDTH - quit_button.w)/2;
quit_button.y = (SCREEN_HEIGHT - quit_button.h)/2;
if (SDL_RenderFillRect(renderer, &quit_button) != 0) {
return_error("Impossible de dessiner le rectangle dans le render");
SDL_Quit();
}
}
I tried several numbers for alpha such as 0, 30 and so on, but it's impossible to edit the opacity of the rectangle.
The red bars a cross the top flickers. I can make them out of the loop but not in the loop.
#include <allegro.h>
void init();
void deinit();
int main() {
init();
int length = 20;
int x= 900;
int y= 600;
int c;
//while (!key[KEY_ESC]) {
c = makecol(0, 0, 0);
rectfill (screen, 0, 0, 900, 400, c);
c = makecol(0, 200, 0);
rectfill (screen, 0, 0, 900, 400, c);// curb
for (int i =0; i=10; i++){
int bar=0;
c = makecol(200, 0, 0);
rectfill (screen, bar, 0, x+80, 40, c);
c = makecol(0, 0, 0);
rectfill (screen, bar+80, 0, x+100, 40, c);
bar+=100;
}
deinit();
return 0;
}
END_OF_MAIN()
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 900, 600, 0,0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}
void deinit() {
clear_keybuf();
/* add other deinitializations here */
}
So what why does it flicker and how do I stop that. I have had
issue with for loops before do they just not work whit allegro?
or am I being dumb and just written something wrong?
I'm creating a menu in my game (available when I hit escape on the keyboard)
But the black background created with SDL_Rect and the text aren't showing and I don't understand why.
When I run my program I can change the background images by pressing on the arrows (up, down, left and right), and when I hit esc, i can't do anything until I hit esc another time, and then I can continue changing the background color with the arrows on my keyboard.
Does anybody can help me with this?
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL_image.h>
#include <SDL2/SDL_ttf.h>
#define CHOICEMENU 2
void errorMessage(const char *message);
void errorMessageImg(const char *message);
void errorMessageTtf(const char *message);
int printTextTtf(SDL_Surface* window, TTF_Font* mainFont, const char* text);
int displayMenu(SDL_Surface* window, TTF_Font* font);
int main( int argc, char **argv)
{
//Pointeurs de la SDL :
SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *image1,*image2, *image3, *image4 = NULL;
SDL_Surface *currentImage = NULL;
SDL_Surface *icon = NULL;
SDL_Renderer *renderer = NULL;
int menu;
int running;
TTF_Init(); //initialisation des textes
TTF_Font *mainFont = NULL;
mainFont = TTF_OpenFont("couriernew.ttf",30);
icon = IMG_Load("img/flask.png");
SDL_SetWindowIcon(window,icon);
if(SDL_Init(SDL_INIT_VIDEO) != 0){ //retourne 0 si Ok
errorMessage("Erreur lors de la creation de la fenêtre et du rendu");
}
window = SDL_CreateWindow("Jeu Antoine et Robin", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);
//texture = SDL_LoadBMP("ici.bmp");
image1 = SDL_LoadBMP("img/image1.bmp");
if(image1 == NULL){
SDL_DestroyWindow(window);
errorMessage("Impossible de charger l'image 1");
}
image2 = SDL_LoadBMP("img/image2.bmp");
if(image2 == NULL){
SDL_DestroyWindow(window);
errorMessage("Impossible de charger l'image 2");
}
image3 = SDL_LoadBMP("img/image3.bmp");
if(image3 == NULL){
SDL_DestroyWindow(window);
errorMessage("Impossible de charger l'image 3");
}
image4 = SDL_LoadBMP("img/image4.bmp");
if(image4 == NULL){
SDL_DestroyWindow(window);
errorMessage("Impossible de charger l'image 4");
}
currentImage = image1;
int isRunning = 1;
SDL_Event ev;
while(isRunning == 1){
while(SDL_PollEvent(&ev) != 0 ){
if(ev.type == SDL_QUIT){
isRunning = false;
}else if(ev.type == SDL_KEYDOWN){
switch(ev.key.keysym.sym){
case SDLK_UP:
currentImage = image1;
break;
case SDLK_RIGHT:
currentImage = image2;
break;
case SDLK_LEFT:
currentImage = image3;
break;
case SDLK_DOWN:
currentImage = image4;
break;
case SDL_QUIT:
isRunning = 0;
break;
case SDLK_ESCAPE:
menu = displayMenu(windowSurface,mainFont);
if(menu == 1){
isRunning = false;
}
break;
}
}
}
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
}
TTF_CloseFont(mainFont);
SDL_FreeSurface(image1);
SDL_FreeSurface(image2);
SDL_FreeSurface(image3);
SDL_FreeSurface(image4);
SDL_DestroyWindow(window);
SDL_FreeSurface(icon);
SDL_DestroyRenderer(renderer);
currentImage = image1 = image2 = image3 = NULL;
window = NULL;
SDL_Quit();
return 0;
}
int displayMenu(SDL_Surface* window, TTF_Font* font){
int x,y; // coordonées de la souris
int i;
int nMenu = CHOICEMENU; //nb de choix
const char* choice[CHOICEMENU] = {"Continuer","Quitter"};
SDL_Surface* menus[CHOICEMENU]; // surface des menus
int selected[CHOICEMENU]={0,0}; // 0 car aucun selectionné pour le moment
SDL_Color colorMenu[2]={{127,122,204},{49,100,226}};
menus[0] = TTF_RenderText_Solid(font, choice[0], colorMenu[0]);
menus[1] = TTF_RenderText_Solid(font, choice[1], colorMenu[1]);
SDL_Rect pos[CHOICEMENU];
pos[0].x = window->clip_rect.w/2 /*= centre */ - menus[0]->clip_rect.w/2;
pos[0].y = window->clip_rect.h/2 - menus[0]->clip_rect.h;
pos[1].x = window->clip_rect.w/2 - menus[0]->clip_rect.w/2;
pos[1].y = window->clip_rect.h/2 + menus[0]->clip_rect.h;
SDL_FillRect(window,&window->clip_rect,SDL_MapRGB(window-
>format,0x00,0x00,0x00));
SDL_Event ev;
while(1){
while(SDL_PollEvent(&ev)){
switch(ev.type){
case SDL_QUIT:
for(i = 0; i < CHOICEMENU ; i++){
SDL_FreeSurface(menus[i]);
}
return 1;
case SDL_MOUSEMOTION: // coordonée souris
x = ev.motion.x;
y = ev.motion.y;
for(i = 0; i < CHOICEMENU; i++){
if(x >= pos[i].x && x<= pos[i].x+pos[i].w && y>=pos[i].y &&
y<=pos[i].y+pos[i].h){
if(!selected[i]){
selected[i] = 1;
SDL_FreeSurface(menus[i]); // on suppr l'ancienne
surface
menus[i] = TTF_RenderText_Solid(font, choice[i],
colorMenu[1]);
} else {
if(selected[i]){
selected[i] = 0;
SDL_FreeSurface(menus[i]); // on suppr
l'ancienne surface
menus[i] = TTF_RenderText_Solid(font, choice[i],
colorMenu[0]);
}
}
}
case SDL_MOUSEBUTTONDOWN: //si qqn a cliquer sur la souris :
x = ev.button.x;
y = ev.button.y;
for (i = 0; i < CHOICEMENU; i+=1){
if(x >= pos[i].x && x<= pos[i].x+pos[i].w && y>=pos[i].y &&
y<=pos[i].y+pos[i].h){
for(i = 0 ;i < CHOICEMENU; i++){
SDL_FreeSurface(menus[i]);
}
return i; // On retourne la valeur actuelle : ex : si je
clique sur "continue" ça return 1
}
}
break;
case SDL_KEYDOWN:
if(ev.key.keysym.sym == SDLK_ESCAPE){
for(i = 0 ;i < CHOICEMENU; i++){
SDL_FreeSurface(menus[i]);
}
return 0;
}
}
}
for(i = 0; i < CHOICEMENU; i+=1){
SDL_BlitSurface(menus[i],NULL,window,&pos[i]);
}
}
}
}
I'm trying to develop a little Supervision System with two GtkDrawarea Widgets and update them simultaneously whenever I toogle the buttons regarded for each Drawarea. A little example I made for ploting two sinoides.
However just the first GtkDrawarea Widget are updated in did, the other remains freezed until I rezise the GtkWindow. I tried use g_timeout_add_full but I seens to work only with one GtkDrawArea.
I'm sending an image of my test aplication.
If someone could help me I will be very thankful.
PS: Sorry for my English :-)
Here is my code:
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
GtkWidget *window;
GtkWidget *darea1;
GtkWidget *darea2;
double w_larg = 1600;
double w_alt = 1200;
bool toggle_stat1 = false;
bool toggle_stat2 = false;
int width, height;
double fase1 = 2;
double amp1 = 1.25;
double fase2 = 4;
double amp2 = 1.25;
gboolean flag_amp1 = TRUE;
gboolean flag_amp2 = TRUE;
static gboolean invalidate_drawing1(gpointer data);
static gboolean invalidate_drawing2(gpointer data);
//*******************************************************************************
static gboolean on_toggled1(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat1 = true;
printf("ToggleButton01 --> Acionado / toggle_stat1 = %d\n",toggle_stat1);
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,invalidate_drawing1,darea1,NULL); // Variação atualizada da linha acima :-)
}
else
{toggle_stat1 = false;
printf("ToggleButton01 --> Liberado / toggle_stat1 = %d\n",toggle_stat1);
}
return FALSE;
}
//*******************************************************************************
static gboolean on_toggled2(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat2 = true;
printf("ToggleButton02 --> Acionado / toggle_stat2 = %d\n",toggle_stat2);
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,invalidate_drawing2,darea2,NULL); // Variação atualizada da linha acima :-)
}
else
{toggle_stat2 = false;
printf("ToggleButton02 --> Liberado / toggle_stat2 = %d\n",toggle_stat2);
}
return FALSE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea1(GtkWidget *widget, cairo_t *cr, gpointer data)
{
int valor1;
double valor2;
static const double dashed1[] = {4.0, 21.0, 2.0};
static const double dashed3[] = {2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase1*M_PI/i;
double y;
double h = 0;
if (amp1 != 0) h = h + j/amp1;
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 0, 0, 1, 1);
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+2)
{
valor1 = rand()%5;
valor2 = rand()%50;
y=x*sc;
if (valor1 < 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) + valor2 );
if (valor1 == 3) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
if (valor1 > 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) - valor2 );
}
cairo_stroke(cr);
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 1, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1);
cairo_set_dash(cr,dashed3, 1, 0 );
cairo_set_line_width(cr, 1.0);
cairo_move_to(cr,0,(j/2));
cairo_line_to(cr,i,(j/2));
cairo_move_to(cr,(i/2),0);
cairo_line_to(cr,(i/2),j);
cairo_stroke(cr);
cairo_destroy(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea2(GtkWidget *widget, cairo_t *cr, gpointer data)
{
int valor1;
double valor2;
static const double dashed1[] = {4.0, 21.0, 2.0};
static const double dashed3[] = {2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase2*M_PI/i;
double y;
double h = 0;
if (amp2 != 0) h = h + j/amp2;
//---------------------
cairo_set_source_rgba(cr, 0, 0, 1, 1);
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+2)
{
valor1 = rand()%5;
valor2 = rand()%50;
y=x*sc;
if (valor1 < 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) + valor2 );
if (valor1 == 3) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
if (valor1 > 3 ) cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) - valor2 );
}
cairo_stroke(cr);
//---------------------
cairo_set_source_rgba(cr, 0, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
//---------------------
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1);
cairo_set_dash(cr,dashed3, 1, 0 );
cairo_set_line_width(cr, 1.0);
cairo_move_to(cr,0,(j/2));
cairo_line_to(cr,i,(j/2));
cairo_move_to(cr,(i/2),0);
cairo_line_to(cr,(i/2),j);
cairo_stroke(cr);
cairo_destroy(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
//*******************************************************************************
static gboolean invalidate_drawing1(gpointer data)
{
GdkWindow *win;
g_print("T_Signal-1 | amp1 = %2.3f | ",amp1);
if (toggle_stat1==true)
{
if ((flag_amp1==TRUE)&&(amp1 <= 3))
amp1=amp1+0.25;
else
flag_amp1=FALSE;
if ((flag_amp1==FALSE)&&(amp1 >= 1.25))
amp1=amp1-0.25;
else
flag_amp1=TRUE;
}
//---------------------
win = gtk_widget_get_window(GTK_WIDGET(data));
if (win) {
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(data), &allocation);
gdk_window_invalidate_rect(win, &allocation, FALSE);
}
if (flag_amp1 == TRUE)
return TRUE;
else
return FALSE;
}
//**********************
//**********************
//**********************
static gboolean invalidate_drawing2(gpointer data)
{
GdkWindow *win;
//---------------------
g_print("T_Signal-2 | amp2 = %2.3f\n",amp2);
if (toggle_stat2==true)
{
if ((flag_amp2==TRUE)&&(amp2 <= 3))
amp2=amp2+0.25;
else
flag_amp2=FALSE;
if ((flag_amp2==FALSE)&&(amp2 >= 1.25))
amp2=amp2-0.25;
else
flag_amp2=TRUE;
}
//---------------------
win = gtk_widget_get_window(GTK_WIDGET(data));
if (win) {
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(data), &allocation);
gdk_window_invalidate_rect(win, &allocation, FALSE);
}
if (flag_amp2 == TRUE)
return TRUE;
else
return FALSE;
}
//**********************
//**********************
//**********************
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *togglebutton1;
GtkWidget *togglebutton2;
GtkWidget *togglebutton3;
GtkWidget *button1; // Amp+
GtkWidget *button2; // Amp-
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "GTK_Glade_2DrawArea01-R00.glade", NULL); //Carrega as informações do projeto glade para o GtkBuilder
window = GTK_WIDGET(gtk_builder_get_object (builder, "window1")); //Retorna o objeto Window, para mostrá-lo mais tarde
darea1 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea1")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
darea2 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea2")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
togglebutton1 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton1")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton2 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton2")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton3 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton3")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
button1 = GTK_WIDGET(gtk_builder_get_object (builder, "button1")); //Retorna o objeto button1, para mostrá-lo mais tarde
button2 = GTK_WIDGET(gtk_builder_get_object (builder, "button2")); //Retorna o objeto button2, para mostrá-lo mais tarde
gtk_window_set_title(GTK_WINDOW(window), "GTK 2 Viewports DrawArea 01");
gtk_builder_connect_signals (builder, NULL); //Conecta todos os eventos dentro do projeto do glade.
g_object_unref (G_OBJECT (builder)); //Exclui o GtkBuilder, pois não iremos mais usá-lo nesse exemplo.
g_signal_connect(G_OBJECT(togglebutton1) , "toggled" , G_CALLBACK(on_toggled1) , NULL);
g_signal_connect(G_OBJECT(togglebutton2) , "toggled" , G_CALLBACK(on_toggled2) , NULL);
g_signal_connect(G_OBJECT(darea1), "expose-event", G_CALLBACK(draw_darea1), NULL);
g_signal_connect(G_OBJECT(darea2), "expose-event", G_CALLBACK(draw_darea2), NULL);
g_signal_connect_swapped(G_OBJECT(window) , "destroy", G_CALLBACK(gtk_main_quit) , G_OBJECT(window));
gtk_widget_show_all(window); //Mostra a janela com seus objetos
gtk_main ();
return 0;
}
Here's the code fixed (below) after the suggestion andlabs and wogsland. Thank you.
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
GtkWidget *window;
GtkWidget *darea1;
GtkWidget *darea2;
bool toggle_stat1 = false;
bool toggle_stat2 = false;
double fase1 = 2;
double amp1 = 1.25;
double fase2 = 4;
double amp2 = 1.25;
gboolean flag_amp1 = TRUE;
gboolean flag_amp2 = TRUE;
//*******************************************************************************
static gboolean on_toggled1(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat1 = true;
printf("ToggleButton01 --> Acionado / toggle_stat1 = %d\n",toggle_stat1);
}
else
{toggle_stat1 = false;
printf("ToggleButton01 --> Liberado / toggle_stat1 = %d\n",toggle_stat1);
}
return FALSE;
}
//*******************************************************************************
static gboolean on_toggled2(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{toggle_stat2 = true;
printf("ToggleButton02 --> Acionado / toggle_stat2 = %d\n",toggle_stat2);
}
else
{toggle_stat2 = false;
printf("ToggleButton02 --> Liberado / toggle_stat2 = %d\n",toggle_stat2);
}
return FALSE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea1(GtkWidget *widget, cairo_t *cr, gpointer data)
{
static const double dashed1[] = {4.0, 21.0, 2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase1*M_PI/i;
double y;
double h = 0;
if (amp1 != 0) h = h + j/amp1;
//---------------------------------------------------------------------------
cairo_set_source_rgba(cr, 1, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean draw_darea2(GtkWidget *widget, cairo_t *cr, gpointer data)
{
static const double dashed1[] = {4.0, 21.0, 2.0};
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_set_source_rgba(cr, 0.45, 0.45, 0.45, 1);
cairo_rectangle(cr,0 , 0, widget->allocation.width, widget->allocation.height);
cairo_fill(cr);
double i = widget->allocation.width;
double j = widget->allocation.height;
double sc = fase2*M_PI/i;
double y;
double h = 0;
if (amp2 != 0) h = h + j/amp2;
//---------------------
cairo_set_source_rgba(cr, 0, 1, 0, 1);
cairo_set_dash(cr,dashed1, 1, 0 );
cairo_set_line_width(cr, 1.5);
cairo_move_to(cr, 0, (j/2)+(sin( (-i/2)*sc )*(h/2)) );
for(double x=(-i/2);x<=(i/2);x=x+10)
{
y=x*sc;
cairo_line_to(cr,x+(i/2),(j/2)+(sin(y)*(h/2)) );
}
cairo_stroke(cr);
return TRUE;
}
//*******************************************************************************
//*******************************************************************************
static gboolean invalidate_drawing1(GtkWidget *widget)
{
g_print("T_Signal-1 | amp1 = %2.3f | ",amp1);
if (toggle_stat1==true)
{
if ((flag_amp1==TRUE)&&(amp1 <= 3))
amp1=amp1+0.25;
else
flag_amp1=FALSE;
if ((flag_amp1==FALSE)&&(amp1 >= 1.25))
amp1=amp1-0.25;
else
flag_amp1=TRUE;
}
//---------------------
gtk_widget_queue_draw(widget);
return TRUE;
}
//**********************
//**********************
static gboolean invalidate_drawing2(GtkWidget *widget)
{
g_print("T_Signal-2 | amp2 = %2.3f\n",amp2);
if (toggle_stat2==true)
{
if ((flag_amp2==TRUE)&&(amp2 <= 3))
amp2=amp2+0.25;
else
flag_amp2=FALSE;
if ((flag_amp2==FALSE)&&(amp2 >= 1.25))
amp2=amp2-0.25;
else
flag_amp2=TRUE;
}
//---------------------
gtk_widget_queue_draw(widget);
return TRUE;
}
//**********************
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *togglebutton1;
GtkWidget *togglebutton2;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "GTK_Glade_2DrawArea01-R00.glade", NULL); //Carrega as informações do projeto glade para o GtkBuilder
window = GTK_WIDGET(gtk_builder_get_object (builder, "window1")); //Retorna o objeto Window, para mostrá-lo mais tarde
darea1 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea1")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
darea2 = GTK_WIDGET(gtk_builder_get_object (builder, "drawingarea2")); //Retorna o objeto DrawingArea, para mostrá-lo mais tarde
togglebutton1 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton1")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
togglebutton2 = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton2")); //Retorna o objeto togglebutton1, para mostrá-lo mais tarde
gtk_window_set_title(GTK_WINDOW(window), "GTK 2 Viewports DrawArea 01");
gtk_builder_connect_signals (builder, NULL); //Conecta todos os eventos dentro do projeto do glade.
g_object_unref (G_OBJECT (builder)); //Exclui o GtkBuilder, pois não iremos mais usá-lo nesse exemplo.
g_signal_connect(G_OBJECT(togglebutton1) , "toggled" , G_CALLBACK(on_toggled1) , NULL);
g_signal_connect(G_OBJECT(togglebutton2) , "toggled" , G_CALLBACK(on_toggled2) , NULL);
g_signal_connect(G_OBJECT(darea1), "expose-event", G_CALLBACK(draw_darea1), NULL);
g_signal_connect(G_OBJECT(darea2), "expose-event", G_CALLBACK(draw_darea2), NULL);
g_signal_connect_swapped(G_OBJECT(window) , "destroy", G_CALLBACK(gtk_main_quit) , G_OBJECT(window));
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,(GSourceFunc)invalidate_drawing1,(gpointer)darea1,NULL); // Variação atualizada da linha acima :-)
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,250,(GSourceFunc)invalidate_drawing2,(gpointer)darea2,NULL); // Variação atualizada da linha acima :-)
gtk_widget_show_all(window); //Mostra a janela com seus objetos
gtk_main ();
return 0;
}
And the Final Image.
Don't use gdk_window_invalidate_rect(). Use gtk_widget_queue_draw() or gtk_widget_queue_draw_area().
I'm trying to pick up an image from my brand new window and then draw it back to the same window, just to train on XLib.
Here is my code :
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char *argv[]) {
fd_set eventset;
fd_set zeroset;
// struct timeval timeout = {0, 0};
Display *display = 0;
int screen;
Window wnd;
XVisualInfo vinfo;
XSetWindowAttributes attr;
XEvent event;
XImage *bg;
Atom WM_message[2];
int run = 1;
FD_ZERO(&eventset);
FD_ZERO(&zeroset);
if(!(display = XOpenDisplay(0))) {
/* Display not found */
printf("Fail display.\n");
return 0;
}
screen = XDefaultScreen(display);
if(!XMatchVisualInfo(display, screen, 32, TrueColor, &vinfo)) {
if(!XMatchVisualInfo(display, screen, 24, TrueColor, &vinfo)) {
/* No proper color depth available */
XCloseDisplay(display); /* Close X communication */
printf("No found color display. Sorry.\n");
return 0;
}
}
attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
attr.border_pixel = 0;
attr.background_pixel = 0x80000000;
attr.bit_gravity = NorthWestGravity;
attr.win_gravity = NorthWestGravity;
wnd = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 300, 0,
vinfo.depth, InputOutput, vinfo.visual,
CWColormap | CWBorderPixel | CWBackPixel | CWBitGravity | CWWinGravity, &attr);
/* Subscribe to window closing event */
WM_message[0] = XInternAtom(display, "WM_PROTOCOLS", 1);
WM_message[1] = XInternAtom(display, "WM_DELETE_WINDOW", 1);
XSetWMProtocols(display, wnd, WM_message, 2);
XFreeColormap(display, attr.colormap);
XSelectInput(display, wnd, ExposureMask | ButtonPressMask | KeyPressMask);
XMapWindow(display, wnd);
bg = XGetImage(display, XDefaultRootWindow(display), 0, 0, 300, 300, AllPlanes, ZPixmap);
// bg = XGetImage(display, wnd, 100, 100, 100, 100, AllPlanes, ZPixmap);
/* int x;
for(x = 0; x < 10000; x++) {
bg->data[x] = 0x80;
} */
XPutImage(display, wnd, XDefaultGC(display, screen), bg, 0, 0, 0, 0, 300, 300);
// XPutImage(display, wnd, XDefaultGC(display, screen), bg, 100, 100, 100, 100, 100, 100);
XMapWindow(display, wnd);
XFlush(display);
while(run) {
XNextEvent(display, &event);
switch(event.type) {
case Expose:
printf("w = %d, h = %d\n", event.xexpose.width, event.xexpose.height);
break;
case DestroyNotify:
run = 0;
break;
case ClientMessage:
{
if(event.xclient.message_type == WM_message[0]) {
if(event.xclient.data.l[0] == WM_message[1]) {
run = 0;
}
}
}
default:;
}
}
XDestroyImage(bg);
XDestroyWindow(display, wnd);
XCloseDisplay(display);
return 0;
}
This crash my program either on porteus and mobaxterm.
But this lines:
// bg = XGetImage(display, wnd, 100, 100, 100, 100, AllPlanes, ZPixmap);
/* int x;
for(x = 0; x < 10000; x++) {
bg->data[x] = 0x80;
} */
// XPutImage(display, wnd, XDefaultGC(display, screen), bg, 100, 100, 100, 100, 100, 100);
Doesn't crash my program... it just render nothing.
Can someone help me understand why do I experiment this weird behavior of X?
This is the error message I am getting:
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 72 (X_PutImage) Serial number of
failed request: 16 Current serial number in output stream: 18
After further reasearches and tries, I finnally found 2 facts :
First my post is mistaken :
this doesn't crash :
XPutImage(display, XDefaultRootWindow(display), XDefaultGC(display, screen), bg, 100, 100, 100, 100, 100, 100);
Those lines crash :
XPutImage(display, wnd, XDefaultGC(display, screen), bg, 0, 0, 0, 0, 300, 300);
XPutImage(display, wnd, XDefaultGC(display, screen), bg, 100, 100, 100, 100, 100, 100);
Second :
Writing on root window with default gc is alright because default gc is a gc corresponding to the default root window.
But using this GC for my own window is an error since I used color depth that can differ from root window.
So I have now this running well:
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char *argv[]) {
fd_set eventset;
fd_set zeroset;
// struct timeval timeout = {0, 0};
Display *display = 0;
int screen;
Window wnd;
XVisualInfo vinfo;
XSetWindowAttributes attr;
XEvent event;
XImage *bg;
GC mainGC;
Atom WM_message[2];
int run = 1;
FD_ZERO(&eventset);
FD_ZERO(&zeroset);
if(!(display = XOpenDisplay(0))) {
/* Display not found */
printf("Fail display.\n");
return 0;
}
screen = XDefaultScreen(display);
if(!XMatchVisualInfo(display, screen, 32, TrueColor, &vinfo)) {
if(!XMatchVisualInfo(display, screen, 24, TrueColor, &vinfo)) {
/* No proper color depth available */
XCloseDisplay(display); /* Close X communication */
printf("No found color display. Sorry.\n");
return 0;
}
}
attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
attr.border_pixel = 0;
attr.background_pixel = 0x80000000;
attr.bit_gravity = NorthWestGravity;
attr.win_gravity = NorthWestGravity;
wnd = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 300, 0,
vinfo.depth, InputOutput, vinfo.visual,
CWColormap | CWBorderPixel | CWBackPixel | CWBitGravity | CWWinGravity, &attr);
/* Subscribe to window closing event */
WM_message[0] = XInternAtom(display, "WM_PROTOCOLS", 1);
WM_message[1] = XInternAtom(display, "WM_DELETE_WINDOW", 1);
XSetWMProtocols(display, wnd, WM_message, 2);
XFreeColormap(display, attr.colormap);
XSelectInput(display, wnd, ExposureMask | ButtonPressMask | KeyPressMask);
XMapWindow(display, wnd);
XFlush(display);
mainGC = XCreateGC(display, wnd, 0, 0);
bg = XGetImage(display, wnd, 0, 0, 100, 100, AllPlanes, ZPixmap);
int x;
for(x = 0; x < 10000; x++) {
bg->data[x] = 0x80;
}
XPutImage(display, wnd, mainGC, bg, 0, 0, 100, 100, 100, 100);
while(run) {
XNextEvent(display, &event);
switch(event.type) {
case Expose:
printf("w = %d, h = %d\n", event.xexpose.width, event.xexpose.height);
break;
case DestroyNotify:
run = 0;
break;
case ClientMessage:
{
if(event.xclient.message_type == WM_message[0]) {
if(event.xclient.data.l[0] == WM_message[1]) {
run = 0;
}
}
}
default:;
}
}
XDestroyImage(bg);
XDestroyWindow(display, wnd);
XCloseDisplay(display);
return 0;
}