display.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* This file is part of remurder
  2. *
  3. * Copyright (C) 2014 Hein-Pieter van Braam <hp@tmm.cx>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <SDL/SDL.h>
  21. #include "display.h"
  22. SDL_Surface *surface = NULL;
  23. void init_sdl() {
  24. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  25. fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  26. exit(1);
  27. }
  28. atexit(SDL_Quit);
  29. }
  30. void create_window(int width, int height) {
  31. screen = SDL_SetVideoMode(width, height, 16, SDL_SWSURFACE);
  32. if (screen == NULL) {
  33. fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  34. exit(1);
  35. }
  36. }
  37. int check_events() {
  38. SDL_Event e;
  39. while (SDL_PollEvent(&e)) {
  40. switch (e.type) {
  41. case SDL_KEYDOWN:
  42. return 1;
  43. break;
  44. case SDL_QUIT:
  45. exit(0);
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. return 0;
  52. }
  53. void wait_for_keypress() {
  54. int done = 0;
  55. while (!done) {
  56. SDL_Delay(100);
  57. done = check_events();
  58. }
  59. }
  60. void display_buffer(int width, int height, char *cur_buffer, char *prev_buffer, int delay, char* changed_blocks) {
  61. int x, y;
  62. char *inp, *outp;
  63. if (! surface) {
  64. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width * 2, height * 2, 8, 0, 0, 0, 0);
  65. }
  66. if (SDL_SetPalette(surface, SDL_LOGPAL, colors, 0, NCOLORS) < 0) {
  67. fprintf(stderr, "Couldn't set palette\n");
  68. }
  69. SDL_LockSurface(surface);
  70. inp = cur_buffer;
  71. outp = surface->pixels;
  72. for(y = 0; y < height / 8; ++y) {
  73. for(x = 0; x < width / 8; ++x) {
  74. if(*changed_blocks) {
  75. for(int k = 0; k < 8; ++k) {
  76. for(int l = 0; l < 8; ++l) {
  77. memset(outp + (k * (width * 4)) + (l * 2), *(inp + (k * width) + l), 2);
  78. memset(outp + (k * (width * 4)) + (width * 2) + (l * 2), *(inp + (k * width) + l), 2);
  79. }
  80. }
  81. }
  82. inp += 8;
  83. outp += 16;
  84. changed_blocks++;
  85. }
  86. inp += width * 7;
  87. outp += width * 4 * 7;
  88. //outp += width;
  89. //outp += (width * 2) * 7;
  90. }
  91. SDL_UnlockSurface(surface);
  92. if (SDL_BlitSurface(surface, NULL, screen, NULL) < 0) {
  93. fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
  94. }
  95. SDL_Flip(screen);
  96. if(delay == 0)
  97. wait_for_keypress();
  98. else
  99. SDL_Delay(delay);
  100. //SDL_FreeSurface(surface);
  101. check_events();
  102. }