graphic.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #define CHAR_SIZE_W 8
  24. #define CHAR_SIZE_H 16
  25. void putblock(__uint8_t color, int xs, int xe, int ys, int ye) {
  26. int x, y;
  27. char *fb = FB_ADDR;
  28. for (y = ys; y <= ye; y++) {
  29. for (x = xs; x <= xe; x++)
  30. fb[y * VGA_X + x] = color;
  31. }
  32. return;
  33. }
  34. static int x = 0, y = 0;
  35. void putc(char c, __uint8_t fg, __uint8_t bg) {
  36. if (y>320){
  37. y=0;
  38. x++;
  39. //memset(FB_ADDR,0,320);
  40. }
  41. if (c == '\n') {
  42. /* it's '\n', but we use '\r\n' */
  43. x = 0;
  44. y += CHAR_SIZE_H;
  45. return;
  46. }
  47. if (c == '\r') {
  48. x = 0;
  49. return;
  50. }
  51. if (c == ' ') {
  52. x += CHAR_SIZE_W;
  53. return;
  54. }
  55. struct psf_header *psf = (struct psf_header *) &font_start;
  56. /*
  57. * yeah, it's very ugly. but it just get address of this glyph, and check
  58. * 'c' is really in this font.
  59. * */
  60. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  61. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  62. /* okay, now we put it on the screen */
  63. int i, j;
  64. char *p, d, mask;
  65. for (i = 0; i < psf->height; i++) {
  66. p = FB_ADDR + (y + i) * VGA_X + x;
  67. d = glyph[i];
  68. for (j = 0; j < 8; j++) {
  69. mask = 0x80 >> j;
  70. if (d & mask)
  71. p[j] = fg;
  72. else
  73. p[j] = bg;
  74. }
  75. }
  76. x += CHAR_SIZE_W;
  77. if (x >= VGA_X) {
  78. x = 0;
  79. y += CHAR_SIZE_H;
  80. /* but y will > Y_LINES */
  81. }
  82. return;
  83. }
  84. void print(__uint8_t fg, __uint8_t bg, char *s) {
  85. for (; *s; s++)
  86. putc(*s, fg, bg);
  87. return;
  88. }