1 /* Mednafen - Multi-system Emulator
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 
18 #include "mednafen.h"
19 #include <trio/trio.h>
20 
21 #include "video.h"
22 #include "player.h"
23 
24 namespace Mednafen
25 {
26 
27 static std::string AlbumName, Artist, Copyright;
28 static std::vector<std::string> SongNames;
29 static int TotalSongs;
30 
31 template<typename T>
FastDrawLine(T * buf,int32 pitch,uint32 color,uint32 bmatch,uint32 breplace,const int xs,const int ys,const int ydelta)32 static INLINE void FastDrawLine(T* buf, int32 pitch, uint32 color, uint32 bmatch, uint32 breplace, const int xs, const int ys, const int ydelta)
33 {
34  int y = ys;
35  int y_inc = (ydelta < 0) ? -1 : 1;
36  unsigned i_bound = abs(ydelta);	// No +1, or we'll overflow our buffer(we compensate in our calculation of half_point).
37  unsigned half_point = (i_bound + 1) >> 1;
38 
39  for(unsigned i = 0; i <= half_point; i++, y += y_inc)
40  {
41   if(bmatch != ~0U)
42   {
43    uint32 tmpcolor = color;
44 
45    if(buf[xs + y * pitch] == bmatch)
46     tmpcolor = breplace;
47 
48    if(buf[xs + y * pitch] != breplace)
49     buf[xs + y * pitch] = tmpcolor;
50   }
51   else
52    buf[xs + y * pitch] = color;
53  }
54  y -= y_inc;
55  for(unsigned i = half_point; i < i_bound; i++, y += y_inc)
56  {
57   if(bmatch != ~0U)
58   {
59    uint32 tmpcolor = color;
60 
61    if(buf[xs + 1 + y * pitch] == bmatch)
62     tmpcolor = breplace;
63 
64    if(buf[xs + 1+ y * pitch] != breplace)
65     buf[xs + 1 + y * pitch] = tmpcolor;
66   }
67   else
68    buf[xs + 1 + y * pitch] = color;
69  }
70 }
71 
72 template<typename T>
DrawWaveformSub(T * const pixels,const int32 pitch,const uint32 lcolor,const uint32 rcolor,const uint32 ccolor,const int32 w,const int32 h,const int32 numchan,const int16 * soundbuf,int32 framecount)73 static void DrawWaveformSub(T* const pixels, const int32 pitch, const uint32 lcolor, const uint32 rcolor, const uint32 ccolor, const int32 w, const int32 h, const int32 numchan, const int16* soundbuf, int32 framecount)
74 {
75  // Now we draw the waveform data.  It should be centered vertically, and extend the screen horizontally from left to right.
76  if(framecount)
77  {
78   const int32 x_scale = (framecount << 8) / w;
79   const int32 y_scale = -h;
80   int lastX, lastY;
81 
82   for(int wc = 0; wc < numchan; wc++)
83   {
84    uint32 color = wc ? rcolor : lcolor;
85 
86    if(numchan == 1)
87     color = ccolor;
88 
89    lastX = -1;
90    lastY = 0;
91 
92    for(int x = 0; x < w; x++)
93    {
94     const int32 samp = soundbuf[wc + (x * x_scale >> 8) * numchan];
95     int ypos;
96 
97     ypos = (h / 2) + ((samp * y_scale + 0x4000) >> 15);
98     //ypos = (rand() & 0xFFF) - 0x800;
99 
100     if(ypos < 0)
101      ypos = 0;
102 
103     if(ypos >= h)
104      ypos = h - 1;
105 
106     if(lastX >= 0)
107      FastDrawLine(pixels, pitch, color, wc ? lcolor : ~0U, ccolor, lastX, lastY, ypos - lastY);
108 
109     lastX = x;
110     lastY = ypos;
111    }
112   }
113  }
114 }
115 
DrawWaveform(MDFN_Surface * surface,const MDFN_Rect & dr,const int32 numchan,const int16 * soundbuf,const int32 framecount)116 static INLINE void DrawWaveform(MDFN_Surface* surface, const MDFN_Rect& dr, const int32 numchan, const int16* soundbuf, const int32 framecount)
117 {
118  const uint32 left_color = surface->MakeColor(0x80, 0x80, 0xFF);
119  const uint32 right_color = surface->MakeColor(0x80, 0xFF, 0x80);
120  const uint32 center_color = surface->MakeColor(0x80, 0xCC, 0xCC);
121 
122  switch(surface->format.bpp)
123  {
124   case 8:
125 	// TODO(colors):
126 	DrawWaveformSub(surface->pix<uint8>() + dr.x + dr.y * surface->pitchinpix, surface->pitchinpix, left_color, right_color, center_color, dr.w, dr.h, numchan, soundbuf, framecount);
127 	break;
128 
129   case 16:
130 	DrawWaveformSub(surface->pix<uint16>() + dr.x + dr.y * surface->pitchinpix, surface->pitchinpix, left_color, right_color, center_color, dr.w, dr.h, numchan, soundbuf, framecount);
131 	break;
132 
133   case 32:
134 	DrawWaveformSub(surface->pix<uint32>() + dr.x + dr.y * surface->pitchinpix, surface->pitchinpix, left_color, right_color, center_color, dr.w, dr.h, numchan, soundbuf, framecount);
135 	break;
136  }
137 }
138 
Player_Init(int tsongs,const std::string & album,const std::string & artist,const std::string & copyright,const std::vector<std::string> & snames,bool override_gi)139 void Player_Init(int tsongs, const std::string &album, const std::string &artist, const std::string &copyright, const std::vector<std::string> &snames, bool override_gi)
140 {
141  AlbumName = album;
142  Artist = artist;
143  Copyright = copyright;
144  SongNames = snames;
145 
146  TotalSongs = tsongs;
147 
148  if(override_gi)
149  {
150   MDFNGameInfo->nominal_width = 384;
151   MDFNGameInfo->nominal_height = 240;
152 
153   MDFNGameInfo->fb_width = 384;
154   MDFNGameInfo->fb_height = 240;
155 
156   MDFNGameInfo->lcm_width = 384;
157   MDFNGameInfo->lcm_height = 240;
158 
159   MDFNGameInfo->GameType = GMT_PLAYER;
160  }
161 }
162 
Player_Draw(MDFN_Surface * surface,MDFN_Rect * dr,int CurrentSong,int16 * soundbuf,int32 framecount)163 void Player_Draw(MDFN_Surface* surface, MDFN_Rect* dr, int CurrentSong, int16* soundbuf, int32 framecount)
164 {
165  const unsigned fontid = MDFN_FONT_9x18_18x18;
166  const uint32 text_color = surface->MakeColor(0xE8, 0xE8, 0xE8);
167  const uint32 text_shadow_color = surface->MakeColor(0x00, 0x18, 0x10);
168  //const uint32 bg_color = surface->MakeColor(0x20, 0x00, 0x08);
169 
170  dr->x = 0;
171  dr->y = 0;
172  dr->w = surface->w;
173  dr->h = surface->h;
174 
175  //
176  // Draw the background color
177  //
178  surface->Fill(0x20, 0x00, 0x08, 0);	// FIXME: Fill() changes
179 
180  DrawWaveform(surface, *dr, MDFNGameInfo->soundchan, soundbuf, framecount);
181 
182  //
183  //
184  int32 text_y = 2;
185 
186  DrawTextShadow(surface, 0, text_y, AlbumName, text_color, text_shadow_color, fontid, dr->w);
187  text_y += (13 + 2);
188 
189  DrawTextShadow(surface, 0, text_y, Artist, text_color, text_shadow_color, fontid, dr->w);
190  text_y += (13 + 2);
191 
192  DrawTextShadow(surface, 0, text_y, Copyright, text_color, text_shadow_color, fontid, dr->w);
193  text_y += (13 * 2);
194 
195  // If each song has an individual name, show this song's name.
196  {
197   std::string tmpsong = "";
198 
199   if((unsigned int)CurrentSong < SongNames.size())
200    tmpsong = SongNames[CurrentSong];
201 
202   if(tmpsong == "" && TotalSongs > 1)
203    tmpsong = std::string(_("Song:"));
204 
205   DrawTextShadow(surface, 0, text_y, tmpsong, text_color, text_shadow_color, fontid, dr->w);
206  }
207  text_y += (13 + 2);
208 
209  if(TotalSongs > 1)
210  {
211   char snbuf[32];
212   trio_snprintf(snbuf, 32, "<%d/%d>", CurrentSong + 1, TotalSongs);
213   DrawTextShadow(surface, 0, text_y, snbuf, text_color, text_shadow_color, fontid, dr->w);
214  }
215 }
216 
217 }
218