graphic.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX 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. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/types.h>
  21. #include <kernel/errno.h>
  22. #include <kernel/graphic.h>
  23. void putblock(__uint8_t color, int xs, int xe, int ys, int ye) {
  24. int x, y;
  25. char *fb = FB_ADDR;
  26. for (y = ys; y <= ye; y++) {
  27. for (x = xs; x <= xe; x++)
  28. fb[y * VGA_X + x] = color;
  29. }
  30. return;
  31. }
  32. static int x = 0, y = 0;
  33. void putc(char c, __uint8_t fg, __uint8_t bg) {
  34. if (c == '\n') {
  35. /* it's '\n', but we use '\r\n' */
  36. x = 0;
  37. y += 16;
  38. return;
  39. }
  40. if (c == '\r') {
  41. x = 0;
  42. return;
  43. }
  44. if (c == ' ') {
  45. x += 8;
  46. return;
  47. }
  48. struct psf_header *psf = (struct psf_header *) &font_start;
  49. /*
  50. * yeah, it's very ugly. but it just get address of this glyph, and check
  51. * 'c' is really in this font.
  52. * */
  53. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  54. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  55. /* okay, now we put it on the screen */
  56. int i, j;
  57. char *p, d, mask;
  58. for (i = 0; i < psf->height; i++) {
  59. p = FB_ADDR + (y + i) * VGA_X + x;
  60. d = glyph[i];
  61. for (j = 0; j < 8; j++) {
  62. mask = 0x80 >> j;
  63. if (d & mask)
  64. p[j] = fg;
  65. else
  66. p[j] = bg;
  67. }
  68. }
  69. x += 8;
  70. if (x >= VGA_X) {
  71. x = 0;
  72. y += 16;
  73. /* but y will > Y_LINES */
  74. }
  75. return;
  76. }
  77. void print(__uint8_t fg, __uint8_t bg, char *s) {
  78. for (; *s; s++)
  79. putc(*s, fg, bg);
  80. return;
  81. }