Thread [SDL] Raute und Polygone zeichnen?
(5 answers)
Opened by hlubenow at 2018-01-27 11:59
Es kommt noch besser! Bin stolz wie Bolle :) :
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #!/usr/bin/perl use warnings; use strict; use SDL; use SDL::Events; use SDLx::App; my $WIDTH = 800; my $HEIGHT = 600; my $roadW = 2000; my $segL = 200; # segment length my $camD = 0.84; # camera depth sub getLines { # Preparing a list of 1600 anonymous hashes containing representations # of lines. This is done before the start of the main-loop. my @l = (); my $i; for $i (1 .. 1600) { my %line = (x => 0, y => 0, z => 0, X => 0, Y => 0, W => 0, curve => 0, spriteX => 0, clip => 0, s => undef, s_width => -1, s_height => -1); $line{z} = $i * $segL; if ($i > 300 && $i < 700) { $line{curve} = 0.5; } if ($i > 750) { $line{y} = sin($i / 30) * 1500; } if ($i % 20 == 0) { $line{spriteX} = -2.5; } push(@l, \%line); } return @l; } sub project { # Changes some values inside the anonymous hashes # from world- to screen coordinates: my ($lineref, $camX, $camY, $camZ) = @_; $lineref->{scale} = $camD / ($lineref->{z} - $camZ); $lineref->{X} = (1 + $lineref->{scale} * ($lineref->{x} - $camX)) * $WIDTH / 2; $lineref->{Y} = (1 - $lineref->{scale} * ($lineref->{y} - $camY)) * $HEIGHT / 2; $lineref->{W} = $lineref->{scale} * $roadW * $WIDTH / 2; } sub drawQuad { # Draws a polygone, filled with a color. # The polygone is defined by four points. # Landscape and road are drawn with these polygones. # So this is the central drawing routine. my ($surface, $color, $x1, $y1, $w1, $x2, $y2, $w2) = @_; my @points = ([$x1 - $w1, $y1], [$x2 - $w2, $y2], [$x2 + $w2, $y2], [$x1 + $w1, $y1]); my $pointnr = @points; my $i; my @xs = (); my @ys = (); foreach $i (@points) { my @arr = @{$i}; push(@xs, $arr[0]); push(@ys, $arr[1]); } my $xsref = \@xs; my $ysref = \@ys; SDL::GFX::Primitives::filled_polygon_color($surface, $xsref, $ysref, $pointnr, $color); } sub getColor { # Converts RGB-values to a hex-string: my ($r, $g, $b) = @_; # FF at the end for Alpha == 255: return sprintf("0x%02lX%02lX%02lXFF", $r, $g, $b); } # Initialisation: # Put the window somewhere in the middle: $ENV{SDL_VIDEO_WINDOW_POS} = "176,82"; SDL::init(SDL_INIT_VIDEO); my $app = SDLx::App->new( title => "Outrun Racing", width => $WIDTH, height => $HEIGHT, dt => 0.17, depth => 32, ); my $event = SDL::Event->new(); # Getting 1600 anonymous hashes for lines: my @lines = getLines(); # Main-Loop: my $N = @lines; my $n; my $pos = 0; my $playerX = 0; my $flying = 0; my $running = 1; while ($running) { my $x = 0; my $dx = 0; my $maxy = $HEIGHT; # A bit of delay, to not go too fast :) : SDL::delay(7); # The example in the documentation for using "SDL::Events" # is not good. Use this code instead. # It handles several keys at once, non-blocking: SDL::Events::pump_events(); my $keys_ref = SDL::Events::get_key_state(); my @keys = @{$keys_ref}; if ($keys[SDLK_LEFT] == 1) { $playerX -= 200; } if ($keys[SDLK_RIGHT] == 1) { $playerX += 200; } if ($keys[SDLK_UP] == 1) { $pos += 200; } if ($keys[SDLK_DOWN] == 1) { $pos -= 200; } if ($keys[SDLK_q] == 1) { $flying += 200; } if ($keys[SDLK_a] == 1) { $flying -= 200; } if ($keys[SDLK_ESCAPE] == 1) { $running = 0; } # Some calculations: while ($pos >= $N * $segL) { $pos -= $N * $segL; } while ($pos < 0) { $pos += $N * $segL; } my $startPos = $pos / $segL; if ($flying < 0) { $flying = 0; } my $camH = 1500 + $lines[$startPos]->{y} + $flying; # Drawing the road: foreach $n ($startPos .. $startPos + 300) { my $l = $lines[$n % $N]; if ($n >= $N) { my $n2 = $N * $segL; } else { my $n2 = 0; } project($l, $playerX - $x, $camH, $pos); $x += $dx; $dx += $l->{curve}; if ($l->{Y} >= $maxy) { next; } $maxy = $l->{Y}; my $grass = getColor(0, 154,0); my $rumble = getColor(0, 0, 0); my $road = getColor(105, 105, 105); if (($n / 3) % 2) { $grass = getColor(16, 200, 16); $rumble = getColor(255, 255, 255); $road = getColor(107, 107, 107); } my $p = $lines[($n - 1) % $N]; # previous line drawQuad($app, hex($grass), 0, $p->{Y}, $WIDTH, 0, $l->{Y}, $WIDTH); drawQuad($app, hex($rumble), $p->{X}, $p->{Y}, $p->{W} * 1.2, $l->{X}, $l->{Y}, $l->{W} * 1.2); drawQuad($app, hex($road), $p->{X}, $p->{Y}, $p->{W}, $l->{X}, $l->{Y}, $l->{W}); } # Update the screen: SDL::Video::flip($app); } Übersetzung des Hauptteils von: https://www.youtube.com/watch?v=N60lBZDEwJ8 Cursor-Keys to drive, "q" and "a" to fly. :) Edit am 29.01.2018: Die Klasse "Line" durch "List of Hashes" ersetzt (1.600 Hashes), damit es für euch möglicherweise besser verständlich ist. Last edited: 2018-01-29 02:26:10 +0100 (CET) |