Browse Source

Added some text code for TTF

gfx_backend
Weird Constructor 4 years ago
parent
commit
3e08ca5cde
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. BIN
      DejaVuSansMono-Bold.ttf
  4. BIN
      DejaVuSansMono-BoldOblique.ttf
  5. BIN
      DejaVuSansMono-Oblique.ttf
  6. BIN
      DejaVuSansMono.ttf
  7. BIN
      res/DejaVuSansMono-Bold.ttf
  8. BIN
      res/DejaVuSansMono-BoldOblique.ttf
  9. BIN
      res/DejaVuSansMono-Oblique.ttf
  10. BIN
      res/DejaVuSansMono.ttf
  11. 173
      res/LICENSE.freetype.txt
  12. 31
      res/LICENSE.zlib.txt
  13. BIN
      res/SDL2_ttf-devel-2.0.15-VC.zip
  14. BIN
      res/SDL2_ttf.dll
  15. BIN
      res/SDL2_ttf.lib
  16. BIN
      res/dejavu-fonts-ttf-2.37.zip
  17. BIN
      res/libfreetype-6.dll
  18. BIN
      res/zlib1.dll
  19. 64
      src/main.rs

2
Cargo.lock generated

@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "adler32"
version = "1.0.3"

2
Cargo.toml

@ -7,6 +7,6 @@ edition = "2018"
[dependencies.sdl2]
version="0.32"
default-features=false
features=["bundled"]
features=["ttf","bundled"]
#,"image","gfx","ttf"]
#,"mixer"]

BIN
DejaVuSansMono-Bold.ttf

Binary file not shown.

BIN
DejaVuSansMono-BoldOblique.ttf

Binary file not shown.

BIN
DejaVuSansMono-Oblique.ttf

Binary file not shown.

BIN
DejaVuSansMono.ttf

Binary file not shown.

BIN
res/DejaVuSansMono-Bold.ttf

Binary file not shown.

BIN
res/DejaVuSansMono-BoldOblique.ttf

Binary file not shown.

BIN
res/DejaVuSansMono-Oblique.ttf

Binary file not shown.

BIN
res/DejaVuSansMono.ttf

Binary file not shown.

173
res/LICENSE.freetype.txt

@ -0,0 +1,173 @@
The source code to this library used with SDL_ttf can be found here:
https://hg.libsdl.org/SDL_ttf/file/default/external
---
The FreeType Project LICENSE
----------------------------
2006-Jan-27
Copyright 1996-2002, 2006 by
David Turner, Robert Wilhelm, and Werner Lemberg
Introduction
============
The FreeType Project is distributed in several archive packages;
some of them may contain, in addition to the FreeType font engine,
various tools and contributions which rely on, or relate to, the
FreeType Project.
This license applies to all files found in such packages, and
which do not fall under their own explicit license. The license
affects thus the FreeType font engine, the test programs,
documentation and makefiles, at the very least.
This license was inspired by the BSD, Artistic, and IJG
(Independent JPEG Group) licenses, which all encourage inclusion
and use of free software in commercial and freeware products
alike. As a consequence, its main points are that:
o We don't promise that this software works. However, we will be
interested in any kind of bug reports. (`as is' distribution)
o You can use this software for whatever you want, in parts or
full form, without having to pay us. (`royalty-free' usage)
o You may not pretend that you wrote this software. If you use
it, or only parts of it, in a program, you must acknowledge
somewhere in your documentation that you have used the
FreeType code. (`credits')
We specifically permit and encourage the inclusion of this
software, with or without modifications, in commercial products.
We disclaim all warranties covering The FreeType Project and
assume no liability related to The FreeType Project.
Finally, many people asked us for a preferred form for a
credit/disclaimer to use in compliance with this license. We thus
encourage you to use the following text:
"""
Portions of this software are copyright © <year> The FreeType
Project (www.freetype.org). All rights reserved.
"""
Please replace <year> with the value from the FreeType version you
actually use.
Legal Terms
===========
0. Definitions
--------------
Throughout this license, the terms `package', `FreeType Project',
and `FreeType archive' refer to the set of files originally
distributed by the authors (David Turner, Robert Wilhelm, and
Werner Lemberg) as the `FreeType Project', be they named as alpha,
beta or final release.
`You' refers to the licensee, or person using the project, where
`using' is a generic term including compiling the project's source
code as well as linking it to form a `program' or `executable'.
This program is referred to as `a program using the FreeType
engine'.
This license applies to all files distributed in the original
FreeType Project, including all source code, binaries and
documentation, unless otherwise stated in the file in its
original, unmodified form as distributed in the original archive.
If you are unsure whether or not a particular file is covered by
this license, you must contact us to verify this.
The FreeType Project is copyright (C) 1996-2000 by David Turner,
Robert Wilhelm, and Werner Lemberg. All rights reserved except as
specified below.
1. No Warranty
--------------
THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO
USE, OF THE FREETYPE PROJECT.
2. Redistribution
-----------------
This license grants a worldwide, royalty-free, perpetual and
irrevocable right and license to use, execute, perform, compile,
display, copy, create derivative works of, distribute and
sublicense the FreeType Project (in both source and object code
forms) and derivative works thereof for any purpose; and to
authorize others to exercise some or all of the rights granted
herein, subject to the following conditions:
o Redistribution of source code must retain this license file
(`FTL.TXT') unaltered; any additions, deletions or changes to
the original files must be clearly indicated in accompanying
documentation. The copyright notices of the unaltered,
original files must be preserved in all copies of source
files.
o Redistribution in binary form must provide a disclaimer that
states that the software is based in part of the work of the
FreeType Team, in the distribution documentation. We also
encourage you to put an URL to the FreeType web page in your
documentation, though this isn't mandatory.
These conditions apply to any software derived from or based on
the FreeType Project, not just the unmodified files. If you use
our work, you must acknowledge us. However, no fee need be paid
to us.
3. Advertising
--------------
Neither the FreeType authors and contributors nor you shall use
the name of the other for commercial, advertising, or promotional
purposes without specific prior written permission.
We suggest, but do not require, that you use one or more of the
following phrases to refer to this software in your documentation
or advertising materials: `FreeType Project', `FreeType Engine',
`FreeType library', or `FreeType Distribution'.
As you have not signed this license, you are not required to
accept it. However, as the FreeType Project is copyrighted
material, only this license, or another one contracted with the
authors, grants you the right to use, distribute, and modify it.
Therefore, by using, distributing, or modifying the FreeType
Project, you indicate that you understand and accept all the terms
of this license.
4. Contacts
-----------
There are two mailing lists related to FreeType:
o freetype@nongnu.org
Discusses general use and applications of FreeType, as well as
future and wanted additions to the library and distribution.
If you are looking for support, start in this list if you
haven't found anything to help you in the documentation.
o freetype-devel@nongnu.org
Discusses bugs, as well as engine internals, design issues,
specific licenses, porting, etc.
Our home page can be found at
https://www.freetype.org
--- end of FTL.TXT ---

31
res/LICENSE.zlib.txt

@ -0,0 +1,31 @@
The source code to this library used with SDL_ttf can be found here:
https://hg.libsdl.org/SDL_image/file/default/external
---
Copyright notice:
(C) 1995-2010 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
If you use the zlib library in a product, we would appreciate *not* receiving
lengthy legal documents to sign. The sources are provided for free but without
warranty of any kind. The library has been entirely written by Jean-loup
Gailly and Mark Adler; it does not include third-party code.

BIN
res/SDL2_ttf-devel-2.0.15-VC.zip

Binary file not shown.

BIN
res/SDL2_ttf.dll

Binary file not shown.

BIN
res/SDL2_ttf.lib

Binary file not shown.

BIN
res/dejavu-fonts-ttf-2.37.zip

Binary file not shown.

BIN
res/libfreetype-6.dll

Binary file not shown.

BIN
res/zlib1.dll

Binary file not shown.

64
src/main.rs

@ -1,7 +1,9 @@
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::event::WindowEvent;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use sdl2::rect::Rect;
use std::time::{Instant, Duration};
/*
@ -58,6 +60,7 @@ pub fn main() -> Result<(), String> {
let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.resizable()
.opengl()
.build()
.map_err(|e| e.to_string())?;
@ -69,17 +72,58 @@ pub fn main() -> Result<(), String> {
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
let ttf_ctx = sdl2::ttf::init().map_err(|e| e.to_string())?;
let mut font = ttf_ctx.load_font("DejaVuSansMono.ttf", 10).map_err(|e| e.to_string())?;
// font.set_style(sdl2::ttf::FontStyle::BOLD);
let txt_crt = canvas.texture_creator();
let mut cnt = 0;
let mut frame_time = 0;
let mut last_wait_time = 0;
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
let last_frame = Instant::now();
let event = event_pump.wait_event();
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
Event::Window { win_event: w, timestamp: _, window_id: _ } => {
match w {
WindowEvent::Resized(w, h) => {
println!("XHX {},{}", w, h);
},
WindowEvent::SizeChanged(w, h) => {
println!("XHXSC {},{}", w, h);
},
_ => {}
}
},
_ => {}
}
cnt += 1;
canvas.set_draw_color(Color::RGB(255, 0, 255));
canvas.clear();
let sf = font.render(&format!("FOOOäߧO {} / {}|{}", cnt, frame_time, last_wait_time)).blended(Color::RGBA(0, 0, 0, 255)).map_err(|e| e.to_string())?;
let txt = txt_crt.create_texture_from_surface(&sf).map_err(|e| e.to_string())?;
let tq = txt.query();
canvas.copy(&txt, None, Some(Rect::new(10, 10, tq.width, tq.height))).map_err(|e| e.to_string())?;
canvas.present();
frame_time = last_frame.elapsed().as_millis();
last_wait_time = 16 - (frame_time as i64);
if last_wait_time > 0 {
::std::thread::sleep(Duration::from_millis(last_wait_time as u64));
}
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
// The rest of the game loop goes here...
}
Ok(())

Loading…
Cancel
Save