关键词搜索

源码搜索 ×
×

漫话Redis源码之五十一

发布2022-01-09浏览390次

详情内容

这里主要实现LOLWUT相关的命令函数,不难。

  1. /*
  2. * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ----------------------------------------------------------------------------
  30. *
  31. * This file implements the LOLWUT command. The command should do something
  32. * fun and interesting, and should be replaced by a new implementation at
  33. * each new version of Redis.
  34. */
  35. #include "server.h"
  36. #include "lolwut.h"
  37. #include <math.h>
  38. void lolwut5Command(client *c);
  39. void lolwut6Command(client *c);
  40. /* The default target for LOLWUT if no matching version was found.
  41. * This is what unstable versions of Redis will display. */
  42. void lolwutUnstableCommand(client *c) {
  43. sds rendered = sdsnew("Redis ver. ");
  44. rendered = sdscat(rendered,REDIS_VERSION);
  45. rendered = sdscatlen(rendered,"\n",1);
  46. addReplyVerbatim(c,rendered,sdslen(rendered),"txt");
  47. sdsfree(rendered);
  48. }
  49. /* LOLWUT [VERSION <version>] [... version specific arguments ...] */
  50. void lolwutCommand(client *c) {
  51. char *v = REDIS_VERSION;
  52. char verstr[64];
  53. if (c->argc >= 3 && !strcasecmp(c->argv[1]->ptr,"version")) {
  54. long ver;
  55. if (getLongFromObjectOrReply(c,c->argv[2],&ver,NULL) != C_OK) return;
  56. snprintf(verstr,sizeof(verstr),"%u.0.0",(unsigned int)ver);
  57. v = verstr;
  58. /* Adjust argv/argc to filter the "VERSION ..." option, since the
  59. * specific LOLWUT version implementations don't know about it
  60. * and expect their arguments. */
  61. c->argv += 2;
  62. c->argc -= 2;
  63. }
  64. if ((v[0] == '5' && v[1] == '.' && v[2] != '9') ||
  65. (v[0] == '4' && v[1] == '.' && v[2] == '9'))
  66. lolwut5Command(c);
  67. else if ((v[0] == '6' && v[1] == '.' && v[2] != '9') ||
  68. (v[0] == '5' && v[1] == '.' && v[2] == '9'))
  69. lolwut6Command(c);
  70. else
  71. lolwutUnstableCommand(c);
  72. /* Fix back argc/argv in case of VERSION argument. */
  73. if (v == verstr) {
  74. c->argv -= 2;
  75. c->argc += 2;
  76. }
  77. }
  78. /* ========================== LOLWUT Canvase ===============================
  79. * Many LOLWUT versions will likely print some computer art to the screen.
  80. * This is the case with LOLWUT 5 and LOLWUT 6, so here there is a generic
  81. * canvas implementation that can be reused. */
  82. /* Allocate and return a new canvas of the specified size. */
  83. lwCanvas *lwCreateCanvas(int width, int height, int bgcolor) {
  84. lwCanvas *canvas = zmalloc(sizeof(*canvas));
  85. canvas->width = width;
  86. canvas->height = height;
  87. canvas->pixels = zmalloc((size_t)width*height);
  88. memset(canvas->pixels,bgcolor,(size_t)width*height);
  89. return canvas;
  90. }
  91. /* Free the canvas created by lwCreateCanvas(). */
  92. void lwFreeCanvas(lwCanvas *canvas) {
  93. zfree(canvas->pixels);
  94. zfree(canvas);
  95. }
  96. /* Set a pixel to the specified color. Color is 0 or 1, where zero means no
  97. * dot will be displayed, and 1 means dot will be displayed.
  98. * Coordinates are arranged so that left-top corner is 0,0. You can write
  99. * out of the size of the canvas without issues. */
  100. void lwDrawPixel(lwCanvas *canvas, int x, int y, int color) {
  101. if (x < 0 || x >= canvas->width ||
  102. y < 0 || y >= canvas->height) return;
  103. canvas->pixels[x+y*canvas->width] = color;
  104. }
  105. /* Return the value of the specified pixel on the canvas. */
  106. int lwGetPixel(lwCanvas *canvas, int x, int y) {
  107. if (x < 0 || x >= canvas->width ||
  108. y < 0 || y >= canvas->height) return 0;
  109. return canvas->pixels[x+y*canvas->width];
  110. }
  111. /* Draw a line from x1,y1 to x2,y2 using the Bresenham algorithm. */
  112. void lwDrawLine(lwCanvas *canvas, int x1, int y1, int x2, int y2, int color) {
  113. int dx = abs(x2-x1);
  114. int dy = abs(y2-y1);
  115. int sx = (x1 < x2) ? 1 : -1;
  116. int sy = (y1 < y2) ? 1 : -1;
  117. int err = dx-dy, e2;
  118. while(1) {
  119. lwDrawPixel(canvas,x1,y1,color);
  120. if (x1 == x2 && y1 == y2) break;
  121. e2 = err*2;
  122. if (e2 > -dy) {
  123. err -= dy;
  124. x1 += sx;
  125. }
  126. if (e2 < dx) {
  127. err += dx;
  128. y1 += sy;
  129. }
  130. }
  131. }
  132. /* Draw a square centered at the specified x,y coordinates, with the specified
  133. * rotation angle and size. In order to write a rotated square, we use the
  134. * trivial fact that the parametric equation:
  135. *
  136. * x = sin(k)
  137. * y = cos(k)
  138. *
  139. * Describes a circle for values going from 0 to 2*PI. So basically if we start
  140. * at 45 degrees, that is k = PI/4, with the first point, and then we find
  141. * the other three points incrementing K by PIhttps://files.jxasp.com/image/2 (90 degrees), we'll have the
  142. * points of the square. In order to rotate the square, we just start with
  143. * k = PI/4 + rotation_angle, and we are done.
  144. *
  145. * Of course the vanilla equations above will describe the square inside a
  146. * circle of radius 1, so in order to draw larger squares we'll have to
  147. * multiply the obtained coordinates, and then translate them. However this
  148. * is much simpler than implementing the abstract concept of 2D shape and then
  149. * performing the rotation/translation transformation, so for LOLWUT it's
  150. * a good approach. */
  151. void lwDrawSquare(lwCanvas *canvas, int x, int y, float size, float angle, int color) {
  152. int px[4], py[4];
  153. /* Adjust the desired size according to the fact that the square inscribed
  154. * into a circle of radius 1 has the side of length SQRT(2). This way
  155. * size becomes a simple multiplication factor we can use with our
  156. * coordinates to magnify them. */
  157. size /= 1.4142135623;
  158. size = round(size);
  159. /* Compute the four points. */
  160. float k = M_PI/4 + angle;
  161. for (int j = 0; j < 4; j++) {
  162. px[j] = round(sin(k) * size + x);
  163. py[j] = round(cos(k) * size + y);
  164. k += M_PI/2;
  165. }
  166. /* Draw the square. */
  167. for (int j = 0; j < 4; j++)
  168. lwDrawLine(canvas,px[j],py[j],px[(j+1)%4],py[(j+1)%4],color);
  169. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载