/test/testGtk2.c

https://github.com/onecoolx/picasso · C · 120 lines · 86 code · 34 blank · 0 comment · 0 complexity · 61e796eb76e8e22f00c561c42771f311 MD5 · raw file

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gtk/gtk.h>
  4. #include "picasso.h"
  5. #include "drawFunc.h"
  6. #include "timeuse.h"
  7. GdkPixbuf * pixbuf;
  8. GdkPixbuf * pixa;
  9. GdkPixbuf * pixb;
  10. static ps_context *context;
  11. static ps_canvas *canvas;
  12. gboolean expose (GtkWidget *widget, GdkEventExpose *event)
  13. {
  14. suseconds_t t1, t2;
  15. gdk_pixbuf_fill(pixbuf, 0xFFFFFFFF);
  16. t1 = get_time();
  17. draw_test(0, context);
  18. t2 = get_time();
  19. fprintf(stderr, "draw frame use %.6f ms --- %.6f fps\n", (double)(t2-t1), 1000.0/(t2-t1 ? t2-t1 : 1));
  20. gdk_draw_pixbuf(widget->window, widget->style->white_gc, pixbuf, 0, 0, 0, 0, 640, 480, 0,0,0);
  21. return FALSE;
  22. }
  23. void destroy(GtkWidget *widget, gpointer data)
  24. {
  25. dini_context(context);
  26. ps_context_unref(context);
  27. ps_canvas_unref(canvas);
  28. ps_shutdown();
  29. g_object_unref(pixbuf);
  30. gtk_main_quit();
  31. }
  32. gboolean time_func(gpointer data)
  33. {
  34. timer_action(context);
  35. GtkWidget * p = GTK_WIDGET(data);
  36. gtk_widget_queue_draw(p);
  37. return 1;
  38. }
  39. static void init_pixbuf()
  40. {
  41. GError * e = 0;
  42. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 640, 480);
  43. pixa = gdk_pixbuf_new_from_file("selt2.png", &e);
  44. pixb = gdk_pixbuf_new_from_file("pat.png", &e);
  45. guchar* buf = gdk_pixbuf_get_pixels(pixbuf);
  46. canvas = ps_canvas_create_with_data((ps_byte*)buf, COLOR_FORMAT_RGB, 640, 480, 640*3);
  47. context = ps_context_create(canvas, 0);
  48. init_context(context, canvas, buf);
  49. guchar* bufa = gdk_pixbuf_get_pixels(pixa);
  50. guchar* bufb = gdk_pixbuf_get_pixels(pixb);
  51. set_image_data(bufa, COLOR_FORMAT_RGB, gdk_pixbuf_get_width(pixa),
  52. gdk_pixbuf_get_height(pixa), gdk_pixbuf_get_rowstride(pixa));
  53. set_pattern_data(bufb, COLOR_FORMAT_RGB, gdk_pixbuf_get_width(pixb),
  54. gdk_pixbuf_get_height(pixb), gdk_pixbuf_get_rowstride(pixb));
  55. }
  56. static int __argc = 0;
  57. static const char** __argv = NULL;
  58. int argc(void)
  59. {
  60. return __argc;
  61. }
  62. const char** argv(void)
  63. {
  64. return __argv;
  65. }
  66. int main(int argc, char* argv[])
  67. {
  68. GtkWidget *window;
  69. GtkWidget *drawarea;
  70. __argc = (int)argc;
  71. __argv = (const char**)argv;
  72. gtk_init(&argc, &argv);
  73. ps_initialize();
  74. init_pixbuf();
  75. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  76. drawarea = gtk_drawing_area_new();
  77. gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
  78. g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK (destroy), NULL);
  79. g_signal_connect (G_OBJECT(drawarea), "expose_event", G_CALLBACK (expose), NULL);
  80. g_timeout_add(100, time_func, GTK_WIDGET(drawarea));
  81. gtk_container_add (GTK_CONTAINER (window), drawarea);
  82. gtk_widget_show(drawarea);
  83. gtk_widget_show(window);
  84. gtk_main();
  85. return 0;
  86. }